I am trying to learn how to use the .queue() JQuery method. So I started with a basic animation using only setTimeout. I have the code here:
I am wondering how to achieve this same animation using queues. The reason for this is I want to be able to add a ‘cancel’ button to the page that would completely cancel all future steps. Right now if you press the Start button several times quickly, the setTimeout’s pile on each other and make it look strange.
I tried listing each animation separately in something like:
$('#target').queue(function(){
$(this).delay(1000).fadeIn(1000);
$(this).dequeue();
});
But only the fadeIns and fadeOuts happened at the right time, and not the color and text changes. So I added setTimeout’s inside the queue functions for the color and text changes, and this made the timing work. But then when I called
$('#target').clearQueue();
it only stopped the fadeIns and fadeOuts, while the color and text changes still happened.
To summarize my question:
-
How can I achieve the animation in the link while also having a cancel button that will completely clear all future steps if pressed?
-
If the answer to 1 is to use queue(), then how do I do this correctly (in light of my failed attempts described above)?
Thanks!
Functions like
.html()and.css()don’t use the animation queue, so you should use.queue()to schedule those calls in between other animations, and then use.stop(true, true)to cancel the queue if the start button is pressed again.Absolutely do not mix
setTimeoutwith jQuery animations – it won’t work reliably.See http://jsfiddle.net/alnitak/EKNAd/ for your fiddle corrected to use jQuery animation queues:
Also, I previously posted this function to allow calling any jQuery function as if it were queued:
See http://jsfiddle.net/alnitak/AYMY7/ for your function rewritten to use this: