views:

842

answers:

3

Hi, I'm using jQuery for some simple animation effects once user clicks something. However, if they click again before the first animation has completed, nothing happens. They must wait until the first animation has finished.

Is there a way to interrupt the animation process and begin from scratch when a second click occurs?

Thanks.

+6  A: 

you could use the stop() method

HTML:

<input type="button" value="fade out" id="start">
<input type="button" value="stop" id="stop">
<div style="background-color: blue; height: 100px;">Testing</div>

JS:

$("#start").click(function() { $("div").fadeOut("slow"); });
$("#stop").click(function() { $("div").stop(); });

edit: The above code is tested and works in FF3.5 and IE8

Use that on the element that you would like to interrupt the animation of.

Just remember that the element will stop animating mid-animation. So you might need to reset the CSS after the stop, depending on your circumstances.

peirix
Does stop not work on fadeOut? Nothing seems to be happening..
Dan
A: 

Sounds like a job for dequeue.

Josh Stodola
+1  A: 

you can just stop() before relaunching the animation

$("#start").click(function() { $("div").stop().fadeOut("slow"); });
pixeline