-
-
Save englishextra/f721a0c4d12aa30f74c2e089370e09eb to your computer and use it in GitHub Desktop.
Accurate Javascript setInterval replacement
This file contains hidden or 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
/*! | |
* Accurate Javascript setInterval replacement | |
* gist.github.com/manast/1185904 | |
* gist.github.com/englishextra/f721a0c4d12aa30f74c2e089370e09eb | |
* minified with closure-compiler.appspot.com/home | |
* var timer = new interval(50, function(){ if(1===1){timer.stop(), timer = 0;}}); timer.run(); | |
* The handle will be a number that isn't equal to 0; therefore, 0 makes a handy flag value for "no timer set". | |
* stackoverflow.com/questions/5978519/setinterval-and-how-to-use-clearinterval | |
*/ | |
function interval(d,f){this.baseline=void 0;this.run=function(){void 0===this.baseline&&(this.baseline=(new Date).getTime());f();var c=(new Date).getTime();this.baseline+=d;var b=d-(c-this.baseline);0>b&&(b=0);(function(d){d.timer=setTimeout(function(){d.run(c)},b)}(this))};this.stop=function(){clearTimeout(this.timer)}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.