View Full Version : Can setInterval stop itself?

05-07-2007, 11:00 PM

I'm having some issues in a complex Ajax script and it looks like the repeated calls made by a setInterval stop at some point for no known reason. I'm not calling clearInterval, and there seems to be no fatal javascript error, as other javascript code continues executing and responding to user's actions.

Is there anything that may stop setInterval from calling the given function periodically? Is there anything apart from clearInterval that may remove the interval?

Thanks a lot for any hints.

05-08-2007, 03:01 AM

var interval = setInterval(function() {

if (somecondition)
}, 1000);

05-08-2007, 03:05 AM
are you setting the setInterval to a variable? ie:
var int=setInterval("something()",1000);

if so are you setting it as a global variable or a local variable?

if setting it inside a function (local) then the variable looses its value when the function ends.

if you do

var int=setInterval("something()",1000);

the variable is cleared when the function ends, which is the same as clearing the interval. or setting the variable to nothing.

if you were to do something like so:

<script type=text/javascript>
var int='';
function your function(){

it should work, as the variable int is actually a global variable which will retain its value no matter what, unless you specifically reset its value or the page is refreshed.