1

Consider the following: There is a panel (let it be a div element) who's height is controlled using the hover function. The only css property that is animated is height. Both of the animations (on mouse over height+=500px; on mouse out height-=500px) have duration of 10 seconds. Can u imagine this: douring the period of those 20 seconds, you can 'accidentally' roll the mouse in and out at least 10 times.

What jquery does is it remembers all that happened (with the mouse) and chains a looong list of unnecessary animations.

I need u guys to help me prevent this.

2 Answers 2

1

Use the stop()(docs) method to halt the animation.

Use it as .stop(true,true) to clear the queue, and skip to the end before it begins the new one, though I'm not sure that's what you'd want here.

If your intention is to allow the animation to complete, then use the not-selector(docs) and the animated-selector(docs) along with the is()(docs) method to only run the animation if it is not currently animated.

This will prevent buildup in the queue.

if( $(this).is(':not(:animated)') ) {
     // do the animation
}
Sign up to request clarification or add additional context in comments.

Comments

0

Very simple:

  1. Define a separate boolean variable (lets call it "animating") and set it to true when the animation starts. when the animation ends, set it to false.
  2. In your mouse out / mouse in handlers, ignore any events that occur while animating==true
  3. (Optional): In your animation completed handler, check if the mouse is over or outside the div you are monitoring and trigger the next animation if necessary.

3 Comments

can you please specify the "animation completed handler"
@Birowski: I mean the optional callback function you can pass to any jquery animation function. It will be executed once the animation is over. For example see the "complete" parameter in api.jquery.com/animate
good idea, the final solution comes combined by both the answers :}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.