Created
July 7, 2017 13:48
-
-
Save okj579/23d6d3ba2a326c9fa527df4f56ef2fb3 to your computer and use it in GitHub Desktop.
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
$.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