If there are 2 or 3 or 20 statements using jQuery's
$(function() { ... })
to add functions to be executed when the DOM is ready, will all those functions run in parallel or run in sequence?
If there are 2 or 3 or 20 statements using jQuery's
$(function() { ... })
to add functions to be executed when the DOM is ready, will all those functions run in parallel or run in sequence?
they will be run in sequence. you can put breakpoints in firebug to see this behavior
document.ready
behaves like a normal event in this respect, they happen in a sequence and in the order they were bound. You can see the relevant jQuery core source here:
This is what happens when you do $(function)
:
ready: function( fn ) {
jQuery.bindReady();
if ( jQuery.isReady ) {
fn.call( document, jQuery );
} else if ( readyList ) {
readyList.push( fn );
}
return this;
}
And this happens later, when the "ready" event fires:
if ( readyList ) {
var fn, i = 0;
while ( (fn = readyList[ i++ ]) ) {
fn.call( document, jQuery );
}
readyList = null;
}
If the document's already ready, the function executes immediately, that's the if
part in the first code block above.