Event: Increase robustness of an inner native event in leverageNative
[jquery.git] / src / core / ready-no-deferred.js
blobe46ecacd1663ec5ea35580ad4b93564e7cbad0e9
1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.js";
4 var readyCallbacks = [],
5         whenReady = function( fn ) {
6                 readyCallbacks.push( fn );
7         },
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 );
14                 } );
15         };
17 jQuery.fn.ready = function( fn ) {
18         whenReady( fn );
19         return this;
22 jQuery.extend( {
24         // Is the DOM ready to be used? Set to true once it occurs.
25         isReady: false,
27         // A counter to track how many items to wait for before
28         // the ready event fires. See trac-6781
29         readyWait: 1,
31         ready: function( wait ) {
33                 // Abort if there are pending holds or we're already ready
34                 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
35                         return;
36                 }
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 ) {
43                         return;
44                 }
46                 whenReady = function( fn ) {
47                         readyCallbacks.push( fn );
49                         while ( readyCallbacks.length ) {
50                                 fn = readyCallbacks.shift();
51                                 if ( typeof fn === "function" ) {
52                                         executeReady( fn );
53                                 }
54                         }
55                 };
57                 whenReady();
58         }
59 } );
61 // Make jQuery.ready Promise consumable (gh-1778)
62 jQuery.ready.then = jQuery.fn.ready;
64 /**
65  * The ready event handler and self cleanup method
66  */
67 function completed() {
68         document.removeEventListener( "DOMContentLoaded", completed );
69         window.removeEventListener( "load", completed );
70         jQuery.ready();
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 );
80 } else {
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 );