Created
January 6, 2016 16:17
-
-
Save bxt/3548c5069139c1531412 to your computer and use it in GitHub Desktop.
Javascript Interval class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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