1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.js";
4 var readyCallbacks = [],
5 whenReady = function( fn ) {
6 readyCallbacks.push( fn );
8 executeReady = function( fn ) {
10 // Prevent errors from freezing future callback execution (gh-1823)
11 // Not backwards-compatible as this does not execute sync
12 window.setTimeout( function() {
13 fn.call( document, jQuery );
17 jQuery.fn.ready = function( fn ) {
24 // Is the DOM ready to be used? Set to true once it occurs.
27 // A counter to track how many items to wait for before
28 // the ready event fires. See trac-6781
31 ready: function( wait ) {
33 // Abort if there are pending holds or we're already ready
34 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
38 // Remember that the DOM is ready
39 jQuery.isReady = true;
41 // If a normal DOM Ready event fired, decrement, and wait if need be
42 if ( wait !== true && --jQuery.readyWait > 0 ) {
46 whenReady = function( fn ) {
47 readyCallbacks.push( fn );
49 while ( readyCallbacks.length ) {
50 fn = readyCallbacks.shift();
51 if ( typeof fn === "function" ) {
61 // Make jQuery.ready Promise consumable (gh-1778)
62 jQuery.ready.then = jQuery.fn.ready;
65 * The ready event handler and self cleanup method
67 function completed() {
68 document.removeEventListener( "DOMContentLoaded", completed );
69 window.removeEventListener( "load", completed );
73 // Catch cases where $(document).ready() is called
74 // after the browser event has already occurred.
75 if ( document.readyState !== "loading" ) {
77 // Handle it asynchronously to allow scripts the opportunity to delay ready
78 window.setTimeout( jQuery.ready );
82 // Use the handy event callback
83 document.addEventListener( "DOMContentLoaded", completed );
85 // A fallback to window.onload, that will always work
86 window.addEventListener( "load", completed );