Skip to content

Instantly share code, notes, and snippets.

@bxt
Created January 6, 2016 16:17
Show Gist options
  • Save bxt/3548c5069139c1531412 to your computer and use it in GitHub Desktop.
Save bxt/3548c5069139c1531412 to your computer and use it in GitHub Desktop.
Javascript Interval class
/**
* A resumable interval timer.
* Will call `callback` every `delay` milliseconds.
* Use go() to start and stop() to quit triggering the callback.
*/
function Interval(callback,delay){
this.go = function () {
this._ref = window.setInterval(callback,delay);
return this;
}
this.stop = function() {
window.clearInterval(this._ref);
return this;
}
};
// Example:
var interval = new Interval(function(){
if (!confirm("Keep on asking every 6 seconds?")) {
interval.stop();
}
},6000).go();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment