问题描述
我有以下JavaScript代码段:
I've the following JavaScript snippet:
$("#dashboard").addClass("standby").delay(3000).removeClass("standby");
$(".active").removeClass("active");
$("." + target).addClass("active");
$(".showDiv").removeClass("showDiv").addClass("hide");
$("#" + target).removeClass("hide").addClass("showDiv");
#dashboard
处于 standby 时,它应该处理所有这些CSS类更改.更改之后,它应该再次显示#dashboard
.因此,我在standby
类的添加和删除之间设置了delay()
.为了查看是否有效,我添加了3sek的持续时间太长.
While #dashboard
is in standby, it should handle all this CSS-Class changes. After this changes, it should display the #dashboard
again. So I set delay()
between the add and remove of the standby
-class. To see if it works I added the too long duration of 3sek.
但这并没有延迟!为什么不呢?我没看到...
But it doesn't delay! Why it don't? I don't see it...
推荐答案
delay
仅处理动画管道中经过的动作,并且不会对像这样的即时原子操作的时间产生影响.为了延迟诸如添加或删除类之类的操作,可以使用 setTimeout
.
.delay()方法最适合在排队的jQuery效果之间进行延迟.由于它是有限的(例如,它没有提供消除延迟的方法)..delay()不能替代JavaScript的本机setTimeout函数,该函数可能更适合某些用例.
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()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!