Skip to content

Instantly share code, notes, and snippets.

@forficate
Created July 7, 2014 23:14
Show Gist options
  • Save forficate/628da57a4234e5714d24 to your computer and use it in GitHub Desktop.
Save forficate/628da57a4234e5714d24 to your computer and use it in GitHub Desktop.
Javascript Array delayed interval iterator
//Iterates over each item in an array at a delayed interval (1500ms)
Array.prototype.iterate = function (callback, onFinished) {
var self = this;
(function get(index){
setTimeout(function(){
if(index < self.length) {
callback(self[index], index);
get(index + 1);
} else {
if(typeof onFinished !== 'undefined') onFinished() ;
}
}, 1500);
})(0);
};
//Example usage
var items = ["a", "b", "c"]
items.iterate(function(item, index) {
console.log(item)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment