When does document.ready() get invoked?

后端 未结 2 1915
北荒
北荒 2020-12-06 19:37

In the below parsing phase,

When does $document.ready() get executed?

2条回答
  •  情深已故
    2020-12-06 20:15

    When does $document.ready() get executed?

    .ready() can be executed multiple times

    .ready( handler )
    Returns: jQuery
    Description: Specify a function to execute when the DOM is fully loaded.

    If .ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.

    The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.

    n = -1;
    
    function ready() {
      document.getElementsByTagName("p")[0].textContent += "ready " + ++n + "\n";
    }
    
    $(document).ready(ready);
    
    $(document).ready(function() {
      ready();
      $(document).ready([function() {
        ready()
      },
        function() {
          $(document).ready(function() {
            ready();
            $(document).ready([
              function() {
                ready()
              }, function() {
              ready()
              if (n === 5) $(document).ready(function() {ready()})
            }]);
          
          })
        }
      ])
    })
    
    
    
      

    See soruce at ready.js

    if ( document.readyState === "complete" ||
        ( document.readyState !== "loading" && !document.documentElement.doScroll ) ) {
    
        // Handle it asynchronously to allow scripts the opportunity to delay ready
        window.setTimeout( jQuery.ready );
    
    } else {
    
        // Use the handy event callback
        document.addEventListener( "DOMContentLoaded", completed );
    
        // A fallback to window.onload, that will always work
        window.addEventListener( "load", completed );
    }
    

提交回复
热议问题