Bug Tracker

Opened 11 years ago

Closed 10 years ago

#5261 closed bug (fixed)

ready functions can be called out of order

Reported by: dil Owned by: brandon
Priority: major Milestone: 1.4
Component: event Version: 1.4a1
Keywords: patch Cc:
Blocked by: Blocking:

Description

A ready function added while inside another ready function will get called immediately rather than being added to the readyList. This can make the calls appear to be out-of-order since the rest of the readyList functions are not completed before the new function is executed. This behavior is caused by the ready function setting isReady before running the readyList functions but the ready(fn) call does not check if the ready functions are currently being executed to decide if a new function should be run immediately or added to the readyList. (The 1.3.3 $.each() to while loop patch from [6323] as addressed in #5209 is also required for this to work at all.)

Testcase showing secondary function running immediately rather than waiting for current ready function to complete. Prints "A2 A1" rather than "A1 A2".

jQuery(function(){
   jQuery(function(){
      console.log("A2");
   });
   console.log("A1");
});

Testcase showing secondary function running immediately rather than waiting for current and queued ready functions to complete. Prints "B3 B1 B2" rather than "B1 B2 B3".

jQuery(function(){
   jQuery(function(){
      console.log("B3");
   });
   console.log("B1");
});
jQuery(function(){
   console.log("B2");
});

Patch for ready(fn) from latest SVN code:

--- event.js    (revision 6581)
+++ event.js    (working copy)
@@ -704,8 +704,8 @@
                // Attach the listeners
                bindReady();
 
-               // If the DOM is already ready
-               if ( jQuery.isReady ) {
+               // If the DOM is ready and the readyList has been processed
+               if ( jQuery.isReady && !jQuery.readyList ) {
                        // Execute the function immediately
                        fn.call( document, jQuery );

Change History (1)

comment:1 Changed 10 years ago by john

Resolution: fixed
Status: newclosed
Version: 1.3.21.4a1
Note: See TracTickets for help on using tickets.