admin管理员组

文章数量:1287809

My

 setInterval('name(var)',6000);

won't clear with:

clearInterval('name(var)');

or with:

clearInterval('name(var)');

why?

My

 setInterval('name(var)',6000);

won't clear with:

clearInterval('name(var)');

or with:

clearInterval('name(var)');

why?

Share Improve this question edited Jul 9, 2020 at 10:16 braX 11.8k5 gold badges22 silver badges37 bronze badges asked Feb 24, 2011 at 21:50 Toni Michel CaubetToni Michel Caubet 20.2k58 gold badges217 silver badges387 bronze badges 2
  • 1 Pass functions to setInterval, not strings to be evaled. Passing a string is ugly and inefficient at best and does horrible things to scope at worst. – Quentin Commented Feb 24, 2011 at 21:53
  • i was getting name setInterval error (on firebu) if not..) can you please write the correct syntax for it? – Toni Michel Caubet Commented Feb 24, 2011 at 21:57
Add a ment  | 

4 Answers 4

Reset to default 5

setInterval returns a handle of the interval, so you do it like this:

var myInterval = setInterval(function(){ name(var) }, 6000);
clearInterval(myInterval);

Note: Notice how I used a anonymous function instead of a string too.

You need to store the returned intervalID and pass it to clearInterval later.

var intervalID = setInterval('name(var)',6000);
clearInterval(intervalID);

You need to pass in the interval id returned from setInterval ex:

var id = setInterval('name(var)', 2000);
clearInterval(id);

More information here. http://www.elated./articles/javascript-timers-with-settimeout-and-setinterval/

This will work:

var id = setInterval('name(var)',6000);
clearInterval(id);

本文标签: setintervalSimple question about how to clearInterval() in JavascriptStack Overflow