Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-williams/f5406b8a0435372b29b3 to your computer and use it in GitHub Desktop.
Save andy-williams/f5406b8a0435372b29b3 to your computer and use it in GitHub Desktop.
function iterateArrayWithDelay(argsArr, delay, fn) {
var index = 0;
function next() {
// protect against empty array
if (!argsArr.length) {
return;
}
// call the callback
fn(index, argsArr[index]);
++index;
if(index != argsArr.length) {
setTimeout(next, delay);
}
}
// start the iteration
next();
}
// test
function iterateArrayWithDelayTest() {
var args = [5,4,3,2,1];
iterateArrayWithDelay(args, 1000, function(i, num) {
console.log("index: " + i + ", value: " + num);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment