Last active
August 29, 2015 14:25
-
-
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.
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
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