Jquery delay() function

故事扮演 提交于 2019-11-29 02:12:43

问题


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

The current jquery is as follows...

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

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.


回答1:


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 setTimeout to run some code after a specified interval.

Try this:

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



回答2:


.delay() is not only for animations.

It's for anything in a 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



来源:https://stackoverflow.com/questions/8401308/jquery-delay-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!