Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the order of functions being called in jQuery $(document).ready

Tags:

jquery

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?

like image 361
Ron Harlev Avatar asked Feb 23 '10 17:02

Ron Harlev


People also ask

What is the following jQuery instruction used for $( document .ready function ()?

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.

What does $( document .ready function () do?

$( 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.

What is difference between $( document .ready function () vs $( function ()?

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.


2 Answers

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();
  }
});
like image 117
Alex Sexton Avatar answered Oct 27 '22 17:10

Alex Sexton


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()

like image 35
Juraj Blahunka Avatar answered Oct 27 '22 17:10

Juraj Blahunka