Created
February 26, 2015 13:12
-
-
Save gwendall/35787d6a74763f218f92 to your computer and use it in GitHub Desktop.
Simple Javascript async each loop
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
/* | |
Prevents browsers from crashing with heavy loops | |
*/ | |
eachAsync = function(array, iterator, callback) { | |
var timeout = 0; | |
var counter = 0; | |
function process() { | |
iterator.apply && iterator.apply(this, [counter, array[counter]]); | |
counter += 1; | |
if (counter < array.length) { | |
setTimeout(process, timeout); | |
} else { | |
callback.apply && callback.apply(this, []); | |
} | |
}; | |
setTimeout(process, timeout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment