Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple? question about how to clearInterval() in Javascript

My

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

won't clear with:

clearInterval('name(var)');

or with:

clearInterval('name(var)');

why?

like image 243
Toni Michel Caubet Avatar asked Jun 13 '26 14:06

Toni Michel Caubet


2 Answers

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.

like image 71
JCOC611 Avatar answered Jun 15 '26 05:06

JCOC611


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

var intervalID = setInterval('name(var)',6000);
clearInterval(intervalID);
like image 24
amit_g Avatar answered Jun 15 '26 05:06

amit_g