Call order of jQuery ready callback

后端 未结 3 1358
星月不相逢
星月不相逢 2021-01-20 20:35

If have two javaScript functions in a page which are required to be called when document load is complete. Is is possible that any function can executed first or it will be

相关标签:
3条回答
  • 2021-01-20 21:05

    If you want these functions to be executed in order, why don't you just write:

    $(document).ready(function(){ 
        function1();
        function2();
    });
    
    0 讨论(0)
  • 2021-01-20 21:05

    Better make use of callback function to be sure about the execution order of the functions

    0 讨论(0)
  • 2021-01-20 21:06

    jQuery ready uses the Deferred object system :

    ready: function( fn ) {
        // Add the callback
        jQuery.ready.promise().done( fn );
    
        return this;
    },
    

    (from the source code)

    And the documentation states that

    Callbacks are executed in the order they were added

    So yes, your callbacks will be executed in order of addition.

    0 讨论(0)
提交回复
热议问题