javascript - Call order of jQuery ready callback - Stack Overflow

If have two javaScript functions in a page which are required to be called when document load is plete.

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

So if using jQuery if you have following code:

$(document).ready(function(){ function1(); });

$(document).ready(function(){ function2(); });

Can it happen that function2 is executed first or will function1 always be executed first ?

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

So if using jQuery if you have following code:

$(document).ready(function(){ function1(); });

$(document).ready(function(){ function2(); });

Can it happen that function2 is executed first or will function1 always be executed first ?

Share edited Oct 9, 2012 at 12:10 Denys Séguret 383k90 gold badges810 silver badges775 bronze badges asked Oct 9, 2012 at 9:48 Toseef ZafarToseef Zafar 1,7875 gold badges30 silver badges52 bronze badges 3
  • 1 Your script will be read and executed line by line – Mr. Alien Commented Oct 9, 2012 at 9:50
  • 2 This got nothing to do with pure JavaScript, it's 100% dependent on how jQuery implement the $(document).ready() method. – Shadow Wizzard Commented Oct 9, 2012 at 9:52
  • 1 @Mr.Alien - That's not the question. He basically wants to know the rules of event queuing (if any). – Álvaro González Commented Oct 9, 2012 at 9:53
Add a ment  | 

3 Answers 3

Reset to default 7

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.

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

$(document).ready(function(){ 
    function1();
    function2();
});

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742293894a4416727.html

相关推荐

  • javascript - Call order of jQuery ready callback - Stack Overflow

    If have two javaScript functions in a page which are required to be called when document load is plete.

    17小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信