Skip to content

Instantly share code, notes, and snippets.

@badcrocodile
Created August 19, 2015 01:34
Show Gist options
  • Save badcrocodile/547c5d22a9c36399e6f5 to your computer and use it in GitHub Desktop.
Save badcrocodile/547c5d22a9c36399e6f5 to your computer and use it in GitHub Desktop.
Alternative to setTimeout()
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);
};
// Usage:
interval(function(){
// Code block goes here
// Will execute every 1 second, 10 times
}, 1000, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment