jQuery Threads
Thread-like processes with jQuery Randy Morey, 31.Aug.2009
This experiment demonstrates the concept of a "threaded" each function. While there are no true threads in JavaScript, the setTimeout function allows delayed execution of code. By waiting a specified amount of time between each iteration of an "each" loop, it is possible to improve the apparent performance of processor intensive loops. This is possible because browser execution of JavaScript is not blocked during the period between a call to setTimeout and the time its begins executing its function.
Threaded each is called just like traditional each, with two important optional parameters. The settings of threadedEach accept both "wait", and "after" optional arguments. The "wait" setting is the number of milliseconds to wait between each iteration. The "after" setting is an optional callback to execute when the entire loop is complete.
The JavaScript
$('p').threadedEach(function () {
$(this).text('new text');
})