I need to be able to control the order of items being processed in the $(document).ready
event.
These are multiple controls, loaded from multiple files, all asking to be called in the ready
event. They all start an async call to the server (AJAX). Only after they are all done, I need to do some extra work.
What would be the elegant solution for this?
Projects In JavaScript & JQuery The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.
$( document ).ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.
Asynchronous requests fire in order, but will return in whatever order they complete in first. So there is not a sure fire way to force them to end at the same time, however, you can build rules to only run code after certain groups have returned.
For instance, define a callback function with a set of rules, and pass it to each success
callback for all of your ajax requests.
var completedObject = {};
function groupSuccessCallback() {
// Test for any combination of requirements
if ( completedObject.ajax1 && completedObject.ajax2 ) {
... // Do something that only requires 1 and 2
}
if ( completedObject.ajax1 && completedObject.ajax2 && completedObject.ajax3) {
... // Do something that requires all 3 being done
// your data is available at completedObject.ajax#
}
// Or test for _all_ entries for a dynamic count
var allComplete = true;
for(var i in completedObject) {
if ( completedObject.hasOwnProperty(i) && !completedObject[i] ) {
allComplete = false;
}
}
// Do whatchya need.
if (allComplete) {
alert("bb-b-bb-b-b-b-bbb... that's all folks!");
}
}
Then set the flags inside of your success functions:
// Ajax1
completedObject['anything'] = false; // instantiate a dynamic entry in the object or use an array if you can't use names.
$.ajax({
...,
...,
success: function(data) {
completedObject['anything'] = data || true;
groupSuccessCallback();
}
});
John Resig on function evaluation order in $(document).ready:
Every time a new function is added to the queue, it's just being added to an array - then being popped back off again when executed. Let me know if this does not happen for you.
$.readyList - only in pre 1.4 versions
jQuery exposed a property called $.readyList
, which is an array to allow function orded manipulation, however $.readyList in no longer publicly exposed in jQuery 1.4.0
Hackerish way
you can try $(window).load(function () {});
, it should execute after the whole window has loaded (not when document is ready, as it is with $(document).ready()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With