Add a pause or delay in jQuery

PostFeb 6th, 2009 | Comments (4)
UPDATE: jQuery introduced a .delay() method in version 1.4, so this trick is no longer needed.

Karl Swedberg over at Learning jQuery has a great trick for pausing during a jQuery effect chain.

Here's an example that fades an object in, pauses for 3 seconds, and then fades the object out.

$("#some_object").fadeIn().animate({ opacity: 1.0 },3000).fadeOut();

Notice the animate() in there. All it does is change the opacity of an object from 1.0 to 1.0 (i.e. do nothing) over the span of 3 seconds. Brilliant.

Comments

AndrewFeb 10, 2009
Nice. You could prolly use any jQuery effect that has a speed arguement. Ex- $("#some_object").fadeIn().show(3000).fadeOut();
AndrewMar 2, 2009
I guess .show(3000) doesn't work... :(
VasilyApr 24, 2009
Thanks a lot!
AndrewMay 22, 2009
although any .animate will do, such as .animate({fontSize:"1em"},3000)

Post a comment

Name
URL
Email
Comment