jQuery的.delay方法如何在引擎盖下工作? [英] How does jQuery's .delay method work under-the-hood?

查看:73
本文介绍了jQuery的.delay方法如何在引擎盖下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚看到这个并认为它很酷。

I just saw this and think it's cool.

console.log("Starting...");
$("#my_element")
  .fadeIn()
  .delay(3000)
  .fadeOut();
console.log("Finishing...");

.delay方法如何在引擎盖下工作?我的意思是,它是如何弄清楚如何等待3秒而不是中断主控制流程?

How does the .delay method work under-the-hood? I mean, how does it figure out how to wait 3 seconds but not interrupt the main control flow?

推荐答案

jQuery有内部queue对象,这只是一个数组:

jQuery has an internal "queue" object, that is just an array:

[ nextAction,
  action,
  action,
  lastAction ]

当你使用延迟时,它推动:

function delay( ms ){
   setTimeout( dequeue, ms )
}

这意味着一旦达到延迟,就会超时,然后触发下一个动作。然而,立即发生的操作,如 .css ,请执行:

Meaning that once it gets to the delay, there's a timeout and then the next action is fired. Actions that happen immediately, like .css, however, do:

function css(){
    // do stuff
    dequeue();
}

没有延迟。

这篇关于jQuery的.delay方法如何在引擎盖下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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