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?
You can use jQuery to support both synchronous and asynchronous code, with the `$.
on( "ready", handler ) , deprecated as of jQuery 1.8 and removed in jQuery 3.0.
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.
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.
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.
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