Skip to content

Instantly share code, notes, and snippets.

@okj579
Created July 7, 2017 13:48
Show Gist options
  • Save okj579/23d6d3ba2a326c9fa527df4f56ef2fb3 to your computer and use it in GitHub Desktop.
Save okj579/23d6d3ba2a326c9fa527df4f56ef2fb3 to your computer and use it in GitHub Desktop.
$.fn.asyncEach = function(fn, maxDuration) {
var maxDuration = typeof maxDuration === 'undefined' ? 100 : maxDuration,
elems = Array.prototype.slice.apply(this),
i = 0;
var interval = setInterval(function() {
var end = Date.now() + maxDuration;
while (i < elems.length) {
if (fn.apply(elems[i]) === false) {
clearInterval(interval);
return;
}
if (i++ < elems.length && Date.now() > end) {
return;
}
}
clearInterval(interval);
}, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment