.delay() 和 .setTimeout()

发布于 2024-12-04 23:00:16 字数 298 浏览 1 评论 0原文

根据 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 技术交流群。

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

发布评论

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

评论(5

很酷不放纵 2024-12-11 23:00:16

我认为您发布的内容确实很清楚。

使用 .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.

一腔孤↑勇 2024-12-11 23:00:16

据我了解, .delay() 专门用于在给定 jQuery 队列中的方法之间添加延迟。例如,如果您想在 1 秒内将图像淡入视图,使其可见 5 秒,然后再花一秒将其再次淡出视图,则可以执行以下操作:

$('#image').fadeIn(1000).delay(5000).fadeOut(1000);

在本例中,< code>.delay() 使用起来更直观,因为它专门用于延迟给定 jQuery 队列中的事件。另一方面,setImeout() 是一种本机 JavaScript 方法,并不明确用于队列。如果您希望单击按钮后 1 秒弹出警告框,您可以执行以下操作:

function delayAlert() {
    var x = setTimeout("alert('5 seconds later!')", 5000);
}

<input type="submit" value="Delay!" onclick="delayAlert();" />

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:

$('#image').fadeIn(1000).delay(5000).fadeOut(1000);

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:

function delayAlert() {
    var x = setTimeout("alert('5 seconds later!')", 5000);
}

<input type="submit" value="Delay!" onclick="delayAlert();" />
萌化 2024-12-11 23:00:16

您可以将 delay 与动画一起使用,例如:

$('.message').delay(5000).fadeOut();

您还可以使用 timeOut 来延迟动画的开始,例如:

window.setTimeout(function(){
  $('.message').fadeOut();
}, 5000);

如您所见,使用 更容易延迟 比带有动画的setTimeout

您可以使用 setTimeout 延迟几乎所有内容,但只能使用 delay 延迟动画。非动画的方法不受延迟的影响,因此在隐藏元素之前不会等待一段时间,它会立即隐藏它:

$('.message').delay(5000).hide();

You can use delay with animations, for example:

$('.message').delay(5000).fadeOut();

You can also use timeOut to delay the start of animations, for example:

window.setTimeout(function(){
  $('.message').fadeOut();
}, 5000);

As you see, it's easier to use delay than setTimeout with animations.

You can delay pretty much anything with setTimeout, but you can only delay animations with delay. Methods that aren't animations are not affected by delay, so this would not wait a while before hiding the element, it would hide it immediately:

$('.message').delay(5000).hide();
为人所爱 2024-12-11 23:00:16

.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 with clearTimeout()

走过海棠暮 2024-12-11 23:00:16

Delay() 的另一个副作用:它似乎禁用了隐藏(或淡出等)被延迟的对象的能力,直到延迟结束。

例如,我设置了以下代码(也许 stackoverflow 开发人员会识别 css 名称......)来隐藏“div”:

 $j(document).ready(function(){
   var $messageDiv = $j("<div>").addClass('fading_message')
       .text("my alert message here").hide();
   var $closeSpan = $j("<span>").addClass('notify_close').text("x");
   $closeSpan.click(function() {$j(this).parent().slideUp(400);});
   $messageDiv.append($closeSpan);
   $j('.content_wrapper_div').prepend($messageDiv);
   $messageDiv.fadeTo(500, .9).delay(5000).fadeTo(800,0);
 });

单击跨度(位于“div”中)中的“x”确实会触发关闭点击功能(我在那里测试了警报),但 div 没有按照指示向上滑动。但是,如果我用以下内容替换最后一行:

     $messageDiv.fadeTo(500, .9);

..然后它确实起作用 - 当我单击“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':

 $j(document).ready(function(){
   var $messageDiv = $j("<div>").addClass('fading_message')
       .text("my alert message here").hide();
   var $closeSpan = $j("<span>").addClass('notify_close').text("x");
   $closeSpan.click(function() {$j(this).parent().slideUp(400);});
   $messageDiv.append($closeSpan);
   $j('.content_wrapper_div').prepend($messageDiv);
   $messageDiv.fadeTo(500, .9).delay(5000).fadeTo(800,0);
 });

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:

     $messageDiv.fadeTo(500, .9);

..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.

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