Here’s a very tiny jQuery plugin which makes it possible to delay a jQuery animation (or any function) for any amount of time. When you call one animation after another jQuery automatically queues them up and then executes them in order. This plugin inserts a pseudo-animation into that queue resulting in what seems like a delay.
This post has been updated since it’s original publication.
The Code:
$.fn.delay = function(time, callback){ // Empty function: jQuery.fx.step.delay = function(){}; // Return meaningless animation, (will be added to queue) return this.animate({delay:1}, time, callback); } |
Usage:
$('div') .delay(1000) .animate({height:200}) .delay(1000) .animate({height:400}) .delay(500) .fadeOut() .delay(500) .fadeIn(); |
If you want to delay something which is not an animation then you have to pass it in it’s own function as the second parameter to ‘delay’. You can use the ‘this’ keyword to reference the current subject of the chain within this callback function:
// Change background color of all anchors after 1 second: $('a').delay(1000, function(){ $(this).css({ background: 'red' }); }); |
All the plugin does is fake an animation of a property which doesn’t exist. Assigning an empty function to that property isn’t necessary – it’s only for cleanliness (plus I have a feeling it won’t work in really old browsers without it).
There is a ‘delay’ plugin already made available by Evan Byrne over here, the only problem with it is that it won’t work in the same fashion as above (queuing animations) and it does not make the subject of the chain available through the ‘this’ keyword in the callback (you might argue that this is a feature though). If you don’t want normal delayed actions (adding a callback which changes some CSS, for example) to be appended to the animation queue I suggest using Evan’s.
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Awesome. I’ve been needing something like this over and over. I always end up googling and not finding exactly what I want. I can’t wait to implement it. Thanks!
Great plugin… Could come in handly on many different projects.. Thanks!
🙂 I’ve been using this animation trick for awhile via a meaningless opacity change.
This is a great little wrapper tho.
Beautiful implementation. Well done.
Thanks Gabe, Chris, electBlake and Paul! 🙂
@electBlake, I used to use that method too, but it wasn’t a very reusable technique (opacity didn’t always stay the same).
Hey, James. The chaining is a great and needed upgrade from my jQuery delay plugin. I can see that using it for non-animations works exactly the same though. All in all excellent work! 🙂
Hi,
i’ve found a similar snippet: http://stackoverflow.com/questions/316278/timeout-jquery-effects
What is this different from your solution?
Oh, Thanks!
Thank you very much! Nice work.
P.S. Some time ago I’ve used a delay plugin from EvanBot.com, but with jQuery 1.3 it doesn’t work.
Hey, Mike. I was surprised when I saw your comment in my mailbox. Actually I wasn’t aware that my plugin didn’t work with jQuery 1.3, I just haven’t tried to use it on it yet.
I should probably refer users over here for Jame’s updated version.
James, this plugin is very useful, but i find a serious bug, but may it’s a problem of jQuery.
I have a empty #notice div to put messages inside.
I have links at the left of checkboxes to edit each element of the list.
<a title=”Edit” href=”http://localhost/app/edit/8″>
So each link without js works to edit each element.
If JS is on, when i click these link select the checbox of the element asignated on his href.
I have another links (with the same class .action) to edit all the selected checboxes.
If are no checboxes selected, shows an alert.
Well, the problem is that when the alert meesage still visible (before the end of the delay), the another links don’t work well. It’s like the variable id isn’t assignated.
I think that is because jQuery is not asynchronous and if the delay code don’t end, can’t run the another code.
Sorry for my english, and thanks for your time!
Sweet, just what I was looking for. Will put a link in my source code to an upcoming WordPress theme release.
Works Perfectly! Great Job. Just what i was looking for.
here’s a question..
say you have the delay inside a mouseleave event. how do you clear the delay from being triggered on a mouseenter event? as in, say you have a delay on mouseleave and then mouseenter the div again but with the way the plugin is setup now… it still hides it..
Thanks for the nice little plugin! Great documentation too.
Doesn’t work with the latest version of firefox unfortunately
confirm. not working in FF. any solution ?