Skip to content

Instantly share code, notes, and snippets.

@drphrozen
Created August 23, 2012 06:14
Show Gist options
  • Save drphrozen/3433285 to your computer and use it in GitHub Desktop.
Save drphrozen/3433285 to your computer and use it in GitHub Desktop.
eachWait for jQuery ($.each with setTimeout)
(function( $ ) {
$.eachWait = function(arr, callback, interval, bulksize) {
interval = interval || 10;
bulksize = bulksize || 10;
var index = 0;
var intervalObj = setInterval(function() {
for(var j = 0; j<bulksize; j++) {
if (index < arr.length) {
try {
callback(index, arr[index]);
} catch(e) {
clearInterval(intervalObj);
throw e;
}
} else {
clearInterval(intervalObj);
break;
}
index++;
}
}, interval);
};
})( jQuery );
// Example usage:
$(function() {
var arr = [0,1,2,3,6,5,6,7,8,9];
$.eachWait(arr, function(index, value) {
if(arr[index] != index)
throw "noooo";
$('body').append($('<p/>').text(index));
});
});?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment