Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.