Skip to content

Instantly share code, notes, and snippets.

@blackfyre
Created May 23, 2014 11:28
Show Gist options
  • Save blackfyre/325ad4a5585b6e069429 to your computer and use it in GitHub Desktop.
Save blackfyre/325ad4a5585b6e069429 to your computer and use it in GitHub Desktop.
An interval manager for javascript
/**
* Original by Zirak @ http://stackoverflow.com/a/8636050/1012431
*
* Had some issues with the clear function
*
* @type {{intervals: Array, make: make, clear: clear, clearAll: clearAll}}
*/
var interval = {
//to keep a reference to all the intervals
intervals : [],
//create another interval
make : function (fun, delay) {
var newKey = this.intervals.length +1;
this.intervals[newKey] = setInterval(fun,delay);
return newKey;
},
//clear a single interval
clear : function ( id ) {
clearInterval( this.intervals[id] );
delete this.intervals[id];
return true;
},
//clear all intervals
clearAll : function () {
for (var key in this.intervals) {
clearInterval(this.intervals[key]);
delete this.intervals[key];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment