jQuery 的就绪队列 $(foo) 和 $(bar) 是顺序运行还是并行运行?

发布于 2024-09-10 20:38:51 字数 130 浏览 5 评论 0原文

如果有 2、3 或 20 个语句使用 jQuery

$(function() { ... })

添加要在 DOM 准备就绪时执行的函数,那么所有这些函数是并行运行还是按顺序运行?

If there are 2 or 3 or 20 statements using jQuery's

$(function() { ... })

to add functions to be executed when the DOM is ready, will all those functions run in parallel or run in sequence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生死何惧 2024-09-17 20:38:51

在这方面,document.ready 的行为类似于正常事件,它们按顺序发生并且按照它们绑定的顺序。您可以在此处查看相关的 jQuery 核心源码

这就是当您执行 $(function) 时会发生的情况:

ready: function( fn ) {
    jQuery.bindReady();
    if ( jQuery.isReady ) {
        fn.call( document, jQuery );
    } else if ( readyList ) {
        readyList.push( fn );
    }
    return this;
}

当“ready”事件触发时,这种情况会稍后发生:

if ( readyList ) {
    var fn, i = 0;
    while ( (fn = readyList[ i++ ]) ) {
        fn.call( document, jQuery );
    }
    readyList = null;
}

如果文档已经准备就绪,则该函数会立即执行,这是上面第一个代码块中的 if 部分。

document.ready behaves like a normal event in this respect, they happen in a sequence and in the order they were bound. You can see the relevant jQuery core source here:

This is what happens when you do $(function):

ready: function( fn ) {
    jQuery.bindReady();
    if ( jQuery.isReady ) {
        fn.call( document, jQuery );
    } else if ( readyList ) {
        readyList.push( fn );
    }
    return this;
}

And this happens later, when the "ready" event fires:

if ( readyList ) {
    var fn, i = 0;
    while ( (fn = readyList[ i++ ]) ) {
        fn.call( document, jQuery );
    }
    readyList = null;
}

If the document's already ready, the function executes immediately, that's the if part in the first code block above.

一桥轻雨一伞开 2024-09-17 20:38:51

它们将按顺序运行。您可以在 firebug 中放置断点来查看此行为

they will be run in sequence. you can put breakpoints in firebug to see this behavior

日暮斜阳 2024-09-17 20:38:51

他们将按顺序运行。 Javascript 代码不并行运行。

They will run in sequence. Javascript code doesn't run in parallel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文