.delay() 和 .setTimeout()
根据 jQuery 文档 .delay()
,
.delay() 方法最适合在排队的 jQuery 之间进行延迟 影响。因为它是有限的——例如,它没有提供一种方法 取消延迟——.delay() 不能替代 JavaScript 的原生 setTimeout 函数,这可能更适合某些用途 案例。
有人可以对此进行扩展吗?什么时候使用.delay()
比较合适,什么时候使用.setTimeout()
比较好?
According to jQuery document on .delay()
,
The .delay() method is best for delaying between queued jQuery
effects. Because it is limited—it doesn't, for example, offer a way to
cancel the delay—.delay() is not a replacement for JavaScript's native
setTimeout function, which may be more appropriate for certain use
cases.
Could someone please expand on this? When is it more appropriate to use .delay()
, and when is it better to use .setTimeout()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您发布的内容确实很清楚。
使用
.delay()
实现包括动画在内的 jQuery 效果。setTimeout()
最适合用于其他一切。例如,当您需要在特定的时间触发事件时。I think what you posted explains itself really.
Use
.delay()
for jQuery effects including animations.setTimeout()
is best used for everything else. For example when you need to trigger an event at a certain elapsed time.据我了解,
.delay()
专门用于在给定 jQuery 队列中的方法之间添加延迟。例如,如果您想在 1 秒内将图像淡入视图,使其可见 5 秒,然后再花一秒将其再次淡出视图,则可以执行以下操作:在本例中,< code>.delay() 使用起来更直观,因为它专门用于延迟给定 jQuery 队列中的事件。另一方面,
setImeout()
是一种本机 JavaScript 方法,并不明确用于队列。如果您希望单击按钮后 1 秒弹出警告框,您可以执行以下操作:As far as I understand,
.delay()
is meant specifically for adding a delay between methods in a given jQuery queue. For example, if you wanted to fade an image into view during the span of 1 second, have it visible for 5 seconds, and then spend another second to fade it out of view again, you could do the following:In this instance,
.delay()
is more intuitive to use since it is specifically meant for delaying events in a given jQuery queue.setImeout()
, on the other hand, is a native JavaScript method that isn't intended explicitly for a queue line. If you wanted an alert box to pop up 1 second after clicking on a button, you could do the following:您可以将
delay
与动画一起使用,例如:您还可以使用
timeOut
来延迟动画的开始,例如:如您所见,使用
更容易延迟
比带有动画的setTimeout
。您可以使用
setTimeout
延迟几乎所有内容,但只能使用delay
延迟动画。非动画的方法不受延迟的影响,因此在隐藏元素之前不会等待一段时间,它会立即隐藏它:You can use
delay
with animations, for example:You can also use
timeOut
to delay the start of animations, for example:As you see, it's easier to use
delay
thansetTimeout
with animations.You can delay pretty much anything with
setTimeout
, but you can only delay animations withdelay
. Methods that aren't animations are not affected bydelay
, so this would not wait a while before hiding the element, it would hide it immediately:.delay()
主要用于将动画效果链接在一起,中间有暂停。正如文档中提到的,无法取消延迟。如果您可能想要取消延迟,则应使用
setTimeout()
来代替,这样您就可以使用clearTimeout()
取消它.delay()
is mostly used for chaining together animation effects with pauses in between.As the docs mention, there is no way to cancel the delay. In the case where you may want to cancel the delay, a
setTimeout()
should be used instead so you can cancel it withclearTimeout()
Delay() 的另一个副作用:它似乎禁用了隐藏(或淡出等)被延迟的对象的能力,直到延迟结束。
例如,我设置了以下代码(也许 stackoverflow 开发人员会识别 css 名称......)来隐藏“div”:
单击跨度(位于“div”中)中的“x”确实会触发关闭点击功能(我在那里测试了警报),但 div 没有按照指示向上滑动。但是,如果我用以下内容替换最后一行:
..然后它确实起作用 - 当我单击“x”时,周围的 div 向上滑动并离开。似乎 $messageDiv 上的“delay()”函数的后台运行“锁定”了该对象,因此在延迟完成之前尝试关闭它的单独机制无法执行此操作。
Another side effect of delay(): it seems to disable the ability to hide (or fadeOut, etc) the objecting being delayed, until the delay is over.
For example, I set up the following code (perhaps a stackoverflow developer will recognize the css names....) to hide a 'div':
Clicking the "x" that's in the span (that's in the 'div') did fire off the click function (I tested with an alert in there), but the div didn't slideUp as directed. However, If I replace the last line with this:
..then it did work - when I clicked the "x", the surrounding div slideUp and and away. It seems as if the background running of the "delay()" function on the $messageDiv "locked" that object, so that a separate mechanism trying to close it couldn't do so until the delay was done.