我有以下 JavaScript 片段:
$("#dashboard").addClass("standby").delay(3000).removeClass("standby");
$(".active").removeClass("active");
$("." + target).addClass("active");
$(".showDiv").removeClass("showDiv").addClass("hide");
$("#" + target).removeClass("hide").addClass("showDiv");
当#dashboard
处于待机时,它应该处理所有这些CSS类更改。更改后,它应该再次显示#dashboard
。因此,我在 standby
类的添加和删除之间设置了 delay()
。为了看看它是否有效,我添加了太长的 3sek 持续时间。
但它不会延迟!为什么不呢?我没看到...
最佳答案
delay
只会对通过动画管道的 Action 起作用,并且不会影响像这样的即时原子操作的时间。为了延迟诸如添加或删除类之类的事情,您可以使用 setTimeout
.
the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue.
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.
关于jQuery .delay() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17522406/