jQuery jquery延迟()函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8401308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:28:55  来源:igfitidea点击:

Jquery delay() function

jquerydelay

提问by Phil

I have some jquery and am trying to apply a delay to it but can't seem to get it to work.

我有一些 jquery 并且正在尝试对其应用延迟,但似乎无法使其正常工作。

The current jquery is as follows...

当前jquery如下...

image.css({"visibility" : "hidden"}).removeClass("image-background");

and I have tried ammending this according to the jquery website (http://api.jquery.com/delay/) to apply the delay...

我已经尝试根据 jquery 网站(http://api.jquery.com/delay/)修改它以应用延迟......

image.delay(800).css({"visibility" : "hidden"}).removeClass("image-background");

but this doesn't seem to make any difference.

但这似乎没有任何区别。

Can anyone see a problem with this? Or how I could fix the problem?

任何人都可以看到这个问题吗?或者我怎么能解决这个问题?

Thanks in advance.

提前致谢。

回答by Rory McCrossan

The delay()function only applies to actions queued on the element. Most commonly, but not always, these are actions created by the animate()method. In this case, use setTimeoutto run some code after a specified interval.

delay()函数仅适用于元素上排队的操作。最常见但并非总是如此,这些是由animate()方法创建的操作。在这种情况下,用于setTimeout在指定的时间间隔后运行一些代码。

Try this:

尝试这个:

setTimeout(function() {
    image.css({"visibility" : "hidden"}).removeClass("image-background");
}, 800);

回答by RightSaidFred

.delay()is not only for animations.

.delay()不仅适用于动画。

It's for anything in a queue.

它适用于queue.

image.delay(800)
     .queue(function( nxt ) {
         $(this).css({"visibility":"hidden"}).removeClass("image-background");
         nxt(); // continue the queue
     });

For the down voter:

对于下选民:

HERE'S A DEMO

这是一个演示