Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jQuery fire the ready event asynchronously

In jQuery ready event logic:

// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
    // Handle it asynchronously to allow scripts the opportunity to delay ready
    return setTimeout( jQuery.ready, 1 );
}

Could you explain the comment: "Handle it asynchronously to allow scripts the opportunity to delay ready".

I don't understand what scripts and why to delay ready?

like image 449
johnny Avatar asked Nov 21 '12 22:11

johnny


People also ask

Is jQuery load asynchronous?

You can use jQuery to support both synchronous and asynchronous code, with the `$.

Is jQuery document ready deprecated?

on( "ready", handler ) , deprecated as of jQuery 1.8 and removed in jQuery 3.0.

Can document ready be async?

The documentReady async function returns a promise that will resolve when the DOMContentLoaded event fires in the future (i.e. when the document becomes "ready", a term that jQuery made popular), or resolves immediately if DOMContentLoaded already happened.

Are event listeners asynchronous?

Event handlers are really a form of asynchronous programming: you provide a function (the event handler) that will be called, not right away, but whenever the event happens.


1 Answers

If the ready callback(which fires the readyList) would have fire right away, you couldn't hold it's execution once the DOM is ready with the holdReady function.

jQuery.holdReady( hold )
Description: Holds or releases the execution of jQuery's ready event.

The $.holdReady() method allows the caller to delay jQuery's ready event.

This advanced feature would typically be used by dynamic script loaders that want to load additional JavaScript such as jQuery plugins before allowing the ready event to occur, even though the DOM may be ready.

This method must be called early in the document, such as in the immediately after the jQuery script tag. Calling this method after the ready event has already fired will have no effect.

like image 76
gdoron is supporting Monica Avatar answered Sep 30 '22 07:09

gdoron is supporting Monica