Created
January 17, 2011 09:39
-
-
Save AlyxRen/782658 to your computer and use it in GitHub Desktop.
Awesome Interval machiene
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
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