-1

I have make a animation with a interval. This is my script:

var count = 0; var countSecond = -40; function arrowAnimation() { if (count > 40) { clearInterval(); } $('.list-what-we-do .arrow').css({ top: (count++) + 'px' }); } function arrowAnimationSecond() { if (countSecond > 0) { clearInterval(); } $('.list-what-we-do .arrow').css({ right: (countSecond++) + 'px' }); } setInterval(arrowAnimation, 5); setInterval(arrowAnimationSecond, 5); 

Now my question. How can i stop the interval. I used clearInterval. But that is not working. How can i fix this? Thanks for helping!

1
  • 1
    you are not capturing the return value from set interval and using that as an arguement for clear interval. Commented Dec 21, 2012 at 12:38

2 Answers 2

5

When you use setInterval or setTimeout the return value is a reference; you pass this reference to clearInterval to cancel.

var foo = setTimeout(...); clearTimeout(foo); 
Sign up to request clarification or add additional context in comments.

Comments

0

You have to assign the return value of the setInterval method to a variable

Then you can later clear it using clearInterval

var count = 0; var countSecond = -40; var interval = setInterval(arrowAnimation, 5); var secondInterval = setInterval(arrowAnimationSecond, 5); function arrowAnimation() { if (count > 40) { clearInterval(interval); } $('.list-what-we-do .arrow').css({ top: (count++) + 'px' }); } function arrowAnimationSecond() { if (countSecond > 0) { clearInterval(secondInterval); } $('.list-what-we-do .arrow').css({ right: (countSecond++) + 'px' }); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.