Created
March 24, 2014 18:09
-
-
Save shash7/5377d91a0459827f7691 to your computer and use it in GitHub Desktop.
timeloop | A loop for delaying each iteration of a loop. Soooo meta
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
/* | |
* A loop for delaying each iteration of a loop | |
* How to use? | |
* var time = new TimeLoop(ms, iterations, callback); | |
* time(); // Executes the loop | |
* | |
* Also, the first parameter of the callback function is the counter of the loop | |
*/ | |
var TimeLoop = function(ms, i, callback) { | |
i = i || 1; | |
function loop() { | |
setTimeout(function () { | |
callback(i); | |
i--; | |
if (i > 0) { | |
loop(); | |
} | |
}, ms); | |
} | |
return loop; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment