3

I amm working on rotation of objects around a circle. I want to stop the animation when a box reachs at top, there should be a delay of seconds then start animation until next box reaches on top.

Code Snippet:

if(x==40.109375 && y==218.015625){
  clearInterval(timer);
  timer = setInterval(animate, 1000);
}

x and y are the coordinates of top position

Fiddle

Should stop when reach at red circled position

2 Answers 2

1

You have to set a timeout before the animation starts again. Do it this way :

setTimeout(function(){
    timer = setInterval(animate, 35);
 },1000);

As you mentioned it, weird things happen if the mouse enters/leaves the box many times. To work around it, one solution would be to check the state of timer before changing it. Please see this fiddle :

http://jsfiddle.net/p876D/3/

Or, as you have done it, clearing the timeout would work too

http://jsfiddle.net/p876D/4/

Sign up to request clarification or add additional context in comments.

2 Comments

just focus in the box, let it be stopped once, then loose the focus and again hover, it increases the speed
Indeed ! I will try to find a fix !
0

A timeout would delay the start of the next interval

if(x==40.109375 && y==218.015625){
    clearInterval(timer);
    setTimeout(function() {
        timer = setInterval(animate, 100);
    }, 1000);
}

FIDDLE

Comments

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.