Created
July 7, 2014 23:14
-
-
Save forficate/628da57a4234e5714d24 to your computer and use it in GitHub Desktop.
Javascript Array delayed interval iterator
This file contains hidden or 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
//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