ready-no-deferred.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. define( [
  2. "../core",
  3. "../var/document"
  4. ], function( jQuery, document ) {
  5. "use strict";
  6. var readyCallbacks = [],
  7. whenReady = function( fn ) {
  8. readyCallbacks.push( fn );
  9. },
  10. executeReady = function( fn ) {
  11. // Prevent errors from freezing future callback execution (gh-1823)
  12. // Not backwards-compatible as this does not execute sync
  13. window.setTimeout( function() {
  14. fn.call( document, jQuery );
  15. } );
  16. };
  17. jQuery.fn.ready = function( fn ) {
  18. whenReady( fn );
  19. return this;
  20. };
  21. jQuery.extend( {
  22. // Is the DOM ready to be used? Set to true once it occurs.
  23. isReady: false,
  24. // A counter to track how many items to wait for before
  25. // the ready event fires. See #6781
  26. readyWait: 1,
  27. // Hold (or release) the ready event
  28. holdReady: function( hold ) {
  29. if ( hold ) {
  30. jQuery.readyWait++;
  31. } else {
  32. jQuery.ready( true );
  33. }
  34. },
  35. ready: function( wait ) {
  36. // Abort if there are pending holds or we're already ready
  37. if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
  38. return;
  39. }
  40. // Remember that the DOM is ready
  41. jQuery.isReady = true;
  42. // If a normal DOM Ready event fired, decrement, and wait if need be
  43. if ( wait !== true && --jQuery.readyWait > 0 ) {
  44. return;
  45. }
  46. whenReady = function( fn ) {
  47. readyCallbacks.push( fn );
  48. while ( readyCallbacks.length ) {
  49. fn = readyCallbacks.shift();
  50. if ( jQuery.isFunction( fn ) ) {
  51. executeReady( fn );
  52. }
  53. }
  54. };
  55. whenReady();
  56. }
  57. } );
  58. // Make jQuery.ready Promise consumable (gh-1778)
  59. jQuery.ready.then = jQuery.fn.ready;
  60. /**
  61. * The ready event handler and self cleanup method
  62. */
  63. function completed() {
  64. document.removeEventListener( "DOMContentLoaded", completed );
  65. window.removeEventListener( "load", completed );
  66. jQuery.ready();
  67. }
  68. // Catch cases where $(document).ready() is called
  69. // after the browser event has already occurred.
  70. // Support: IE9-10 only
  71. // Older IE sometimes signals "interactive" too soon
  72. if ( document.readyState === "complete" ||
  73. ( document.readyState !== "loading" && !document.documentElement.doScroll ) ) {
  74. // Handle it asynchronously to allow scripts the opportunity to delay ready
  75. window.setTimeout( jQuery.ready );
  76. } else {
  77. // Use the handy event callback
  78. document.addEventListener( "DOMContentLoaded", completed );
  79. // A fallback to window.onload, that will always work
  80. window.addEventListener( "load", completed );
  81. }
  82. } );