Bug Tracker

Modify

Ticket #6185 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

ready( fn ) should not be synchronous

Reported by: hallettj Owned by:
Priority: Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: ready, synchronous Cc:
Blocking: Blocked by:

Description

I am sure this has been discussed before; but I was not able to find such a discussion.

I noticed that if jQuery.isReady is true when ready( fn ) is called then the given callback is called synchronously. This bit me when I set up a mildly complicated class hierarchy in which I set one class to trigger an event in a ready( fn ) callback on instance initialization and set another class to listen for the same event. This all takes place in a single synchronous chunk of JavaScript where the initialization of one instance creates a new instance of the other.

Because that event registration takes places after the other class is initialized my code ends up missing the event. If ready( fn ) were called asynchronously in every case this would not be a problem.

I will cite the principle of least surprise to make the argument that ready( fn ) should behave as consistently as possible - that is it should invoke its callback asynchronously under all circumstances. Because it does not I 1) was surprised and 2) had to wrap a setTimout() around my event-triggering callback to handle the special case where an instance is initialized after the document is ready.

My proposed change is attached as a patch.

Attachments

0001-ready-fn-should-not-run-synchronously.patch Download (1.4 KB) - added by hallettj 3 years ago.
patch that wraps invocation of a callback with setTimeout()

Change History

Changed 3 years ago by hallettj

patch that wraps invocation of a callback with setTimeout()

comment:1 Changed 3 years ago by john

  • Status changed from new to closed
  • Resolution set to wontfix

While I absolutely do appreciate the patch and test case I'm not convinced by the particular need for this. Considering that the DOM is already ready it would actually be very surprising to have your code execution be delayed by an additional 15ms or so (depending upon the browser). I'm also hesitant to make the change since it's likely that there are already users depending upon the existing behavior (executing synchronously after the DOM is ready).

This would probably be a good topic to bring up in discussion on the jQuery forum.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.