准备好jquery文档的执行顺序 [英] Order of execution of jquery document ready

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

问题描述

假设,我有一个看起来像这样的js文件:

Suppose, I have a js file which looks like this:

$(document).ready(function() { // first task} );
$(document).ready(function() { // second task } );
$(document).ready(function() { // third task } );

加载文件后,任务将按顺序执行.我不明白为什么? 我猜想发生准备就绪"事件时会触发回调方法.如何保留执行顺序?连续的回叫是否在某个地方排队?

Upon loading the file the tasks get executed in order. I am not able to understand why ? I'm guessing that the callback methods are fired when an "on ready" event occurs. How is the execution order being retained ? Are the consecutive call backs being queued up in some place ?

注意:我知道这是一种非常幼稚的编码方式.我编写该代码段只是为了表达我的观点,而不是在我的项目中编写此类代码.

Note: I know that this is a very naive way of coding. I have written the snippet only to get my point across and do not write such code in my projects.

推荐答案

您的处理程序正在 readyList )用于执行 顺序稍后,当document

Your handlers are being pushed into an array (readyList) for executing in order later, when the document is ready.

他们像这样排队 :

readyList.push( fn );

并在准备就绪时执行, :

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

如果文档已经准备就绪,则它们将立即执行,但顺序仍然正确.

If the document is already ready, then they'll execute immediately, which is still in order.

这篇关于准备好jquery文档的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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