Jquery delay() function

Phil picture Phil · Dec 6, 2011 · Viewed 37.9k times · Source

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.

Answer

Rory McCrossan picture Rory McCrossan · Dec 6, 2011

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);