Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 14:25
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 bradoyler/f88300a4a9082d8957a2 to your computer and use it in GitHub Desktop.
Save bradoyler/f88300a4a9082d8957a2 to your computer and use it in GitHub Desktop.
handy for worker process stuff when you want to throttle the frequency of requests.
function delayedIterator(array, interval, callback, done) {
var index = 0;
var iterator = function iterator() {
setTimeout(function () {
if(array[index]) {
callback(array[index], index);
index++;
iterator();
}
else {
done();
}
}, interval);
};
if(!array.isArray) {
iterator();
}
else {
console.error('ERROR: '+ array + ' is not an Array');
return done();
}
};
var list = ['A', 'B', 'C'];
delayedIterator(list, 5000,
function (item, index) {
console.log('### item', item, 'index:', index);
},
function () {
console.log('#### DONE!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment