Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery delay() function

Tags:

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.

like image 303
Phil Avatar asked Dec 06 '11 14:12

Phil


People also ask

What is the use of delay () method in jQuery?

The delay() method sets a timer to delay the execution of the next item in the queue.

Is delay () A event method in jQuery?

Added to jQuery in version 1.4, 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. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .

How do I call a function after 2 seconds in jQuery?

To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.


2 Answers

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);
like image 87
Rory McCrossan Avatar answered Oct 03 '22 05:10

Rory McCrossan


.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

like image 39
RightSaidFred Avatar answered Oct 03 '22 03:10

RightSaidFred