jQuery Async Plugin

by Vincent Robert

This plugin allows to easily code very long-lasting loops in an asynchronous way to avoid losing the browser responsiveness.

Download

jQuery.whileAsync(opts)

This function ease the building of an asynchronnous loop with a while structure.

opts
test
(default: returns true) function to execute for the test part of the while loop
loop
(default: nothing) function to execute as the loop body
end
(default: nothing) function to execute at the end of the loop iteration
delay
(default: 10) delay in milliseconds between each asynchronous calls
bulk
(default: 500) maximum delay in milliseconds the loop is allowed to run without any asynchronous calls. 0 to disable.
var test = jQuery('#TestWhile');
test.empty();

var n = 10;
jQuery.whileAsync({
	delay: 100,
	bulk: 0,
	test: function() { return n > 0 },
	loop: function()
	{
		test.text(test.text() + ' ' + n);
		n--;
	},
	end: function()
	{
		test.text(test.text() + ' END');
	}
})
 

jQuery.eachAsync(array, opts)

This function ease the building of a for-each loop.

array
array or array-like to use for the iteration
opts
loop
function(index, value) (default: nothing) function to execute as the loop body
end
function() (default: nothing) function to execute at the end of the loop iteration
delay
(default: 10) delay in milliseconds between each asynchronous calls
bulk
(default: 500) maximum delay in milliseconds the loop is allowed to run without any asynchronous calls. 0 to disable.
var test = jQuery('#TestEach');
test.empty();

var n = 10;
jQuery.eachAsync([1,2,3,4,5,6,7,8,9,10], {
	delay: 100,
	bulk: 0,
	loop: function(index, value)
	{
		test.text(test.text() + ' ' + value);
	},
	end: function()
	{
		test.text(test.text() + ' END');
	}
})
 

jQuery.fn.eachAsync(opts)

This function is the same as jQuery.eachAsync() but works directly on a jQuery.

opts
loop
function(index, value) (default: nothing) function to execute as the loop body
end
function() (default: nothing) function to execute at the end of the loop iteration
delay
(default: 10) delay in milliseconds between each asynchronous calls
bulk
(default: 500) maximum delay in milliseconds the loop is allowed to run without any asynchronous calls. 0 to disable.
var n = 10;
jQuery('#TestFnEach span').empty().eachAsync({
	delay: 100,
	bulk: 0,
	loop: function(index)
	{
		jQuery(this).text(index+' ');
	}
})