Created
August 23, 2012 06:14
-
-
Save drphrozen/3433285 to your computer and use it in GitHub Desktop.
eachWait for jQuery ($.each with setTimeout)
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
(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