https://github.com/aduth/correctinginterval
An auto-correcting alternative to setInterval
https://github.com/aduth/correctinginterval
Last synced: about 1 year ago
JSON representation
An auto-correcting alternative to setInterval
- Host: GitHub
- URL: https://github.com/aduth/correctinginterval
- Owner: aduth
- License: mit
- Created: 2013-12-09T02:09:19.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-07-27T21:24:14.000Z (almost 9 years ago)
- Last Synced: 2025-04-12T09:55:19.299Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 76.2 KB
- Stars: 59
- Watchers: 4
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/aduth/correctingInterval)
# correctingInterval
correctingInterval exposes two functions, `setCorrectingInterval` and `clearCorrectingInterval`, which serve as drop-in replacements for `setInterval` and `clearInterval`. Unlike `setInterval`, the `setCorrectingInterval` function automatically adjusts to correct subsequent intervals that were delayed due to latency. This is useful if your delayed executions need to occur at predictable intervals. For more information, refer to my related blog post below:
[Creating a self-correcting alternative to JavaScript's setInterval](https://andrewduthie.com/2013/12/31/creating-a-self-correcting-alternative-to-javascripts-setinterval/)
## Usage
### Browser
Download correctingInterval.js to your project or install using Bower (`bower install correctingInterval`). Include the file using a `` tag. Then, use with the same syntax as you would use `setInterval`, e.g.:
```javascript
var startTime = Date.now();
setCorrectingInterval(function() {
console.log((Date.now() - startTime) + 'ms elapsed');
}, 1000);
```
### RequireJS
Download correctingInterval.js to your project or install using Bower (`bower install correctingInterval`). Include the file as a dependency to your module. correctingInterval is passed as an object containing the two functions as object members, e.g.:
```javascript
define([
'vendor/correctingInterval'
], function(ci) {
var startTime = Date.now();
ci.setCorrectingInterval(function() {
console.log((Date.now() - startTime) + 'ms elapsed');
}, 1000);
});
```
### Node.js
Install correctingInterval using npm (`npm install correcting-interval`). Require correctingInterval in your application script. correctingInterval is passed as an object containing the two functions as object members, e.g.:
```javascript
var ci = require('correcting-interval');
var startTime = Date.now();
ci.setCorrectingInterval(function() {
console.log((Date.now() - startTime) + 'ms elapsed');
}, 1000);
```
## Clearing Intervals
Similar to `setInterval`, you can stop running intervals by storing a reference to the value returned by `setCorrectingInterval` and later use as the sole parameter to `clearCorrectingInterval`, e.g.:
```javascript
var intervalId = setCorrectingInterval(function() { }, 1000);
// Later in your script...
clearCorrectingInterval(intervalId);
```
## License
Copyright 2014 Andrew Duthie.
Released freely under the MIT license (refer to LICENSE.txt).