Skip to content

Instantly share code, notes, and snippets.

@vrkansagara
Forked from richardkundl/setInterval.js
Created July 5, 2018 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrkansagara/77f3d9f97240e48dfd95ecf695222fbf to your computer and use it in GitHub Desktop.
Save vrkansagara/77f3d9f97240e48dfd95ecf695222fbf to your computer and use it in GitHub Desktop.
An alternative to Javascript's evil setInterval: - Doesn't care whether the callback is still running or not - Ignores errors - Isn't that flexible Thanks: http://www.thecodeship.com/web-development/alternative-to-javascript-evil-setinterval/
function interval(func, wait, times){
var interv = function(w, t){
return function(){
if(typeof t === "undefined" || t-- > 0){
setTimeout(interv, w);
try{
func.call(null);
}
catch(e){
t = 0;
throw e.toString();
}
}
};
}(wait, times);
setTimeout(interv, wait);
};
interval(function(){
// Code block goes here
}, 1000, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment