jQuery ready回调的调用顺序 [英] Call order of jQuery ready callback

查看:88
本文介绍了jQuery ready回调的调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面中有两个javaScript函数,则在文档加载完成后需要调用它们.是否有可能先执行任何功能,或者将是始终先执行的第一个功能?

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 the first function which is executed first always?

因此,如果您使用的是以下代码,则使用jQuery:

So if using jQuery if you have following code:

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

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

会发生首先执行function2还是总是先执行function1的情况?

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

推荐答案

jQuery ready使用推迟对象系统:

jQuery ready uses the Deferred object system :

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

    return this;
},

(来自源代码)

文档指出

以添加的顺序执行回调

Callbacks are executed in the order they were added

是的,您的回调将按添加顺序执行.

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

这篇关于jQuery ready回调的调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆