Skip to content

Instantly share code, notes, and snippets.

@AlyxRen
Created January 17, 2011 09:39
Show Gist options
  • Save AlyxRen/782658 to your computer and use it in GitHub Desktop.
Save AlyxRen/782658 to your computer and use it in GitHub Desktop.
Awesome Interval machiene
var Interval = (function (und){
return function(fn, time, count){
if (time !== time-0) return null;
if (count !== count-0) count = -1;
switch (count){
case -1:
return setInterval(fn, time);
break;
case 0:
return setTimeout(fn, time);
break;
default:
return times(fn, time, count);
break;
}
}
function times(fn, time, times){
var count = 0, local;
return local = setInterval(function(){
if (++count >= times) clearInterval(local);
fn();
}, time);
}
}());
//Once
Interval(function(){
console.log("cool");
}, 1000, 0 || 1);
//15 times
Interval(function(){
console.log("cool");
}, 1000, 15);
//infinite
Interval(function(){
console.log("cool");
}, 1000, -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment