Skip to content

Instantly share code, notes, and snippets.

@shash7
Created March 24, 2014 18:09
Show Gist options
  • Save shash7/5377d91a0459827f7691 to your computer and use it in GitHub Desktop.
Save shash7/5377d91a0459827f7691 to your computer and use it in GitHub Desktop.
timeloop | A loop for delaying each iteration of a loop. Soooo meta
/*
* 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