Forum Moderators: open

Message Too Old, No Replies

clearTimeout not working.

         

Josh_F

12:53 am on Nov 19, 2003 (gmt 0)

10+ Year Member



The following code is meant to enable and disable a timer called slideTimer. After calling disableSlideMode(), the timer still fires.

var slideTimer;

function enableSlideMode () {
slideMode = Boolean(true);
slideTimer = setTimeout('showNext();', (slideDelay * 1000));
}

function disableSlideMode() {
slideMode = Boolean(false);
clearTimeout (slideTimer);
}

It seems like this could be an issue of scope where it thinks slideTimer is a local variable within enableSlideMode(). However, I start the script with a global declaration of "var slideTimer;". Do I need to do something else to make it truly global?

HocusPocus

4:29 pm on Nov 19, 2003 (gmt 0)

10+ Year Member



Code looks fine to me. Just tested this and seems to work..

<SCRIPT>
var slideTimer;
function enableSlideMode () {
slideMode = Boolean(true);
slideTimer = setTimeout('showNext();', (1000));
alert(slideTimer);
}

function disableSlideMode() {
slideMode = Boolean(false);
clearTimeout (slideTimer);
}
function showNext(){alert('next Slide');slideTimer = setTimeout('showNext();', (1000)); }
</SCRIPT>
<body onload=enableSlideMode()>
<INPUT type=submit onclick=disableSlideMode() value=stop>
</body>

Maybes other parts of your script are misbehaving, which is causing the clearTimeout not to function as you intend.

Rem, that setTimeout is a one-time timer. setInterval can be used for repeated calls.

Josh_F

12:23 am on Nov 25, 2003 (gmt 0)

10+ Year Member



Thanks for the help. You were right, it was somewhere else in the code, and boy was it hard to find. Turns out I had already set the timer once globally, then did so again inside a function. This creates two timers. Problem was I had already tried clearing it twice to no avail, not realizing that difference is scope somehow made it "lose" the handle for the one defined within the function. It's as if the variable was redefined locally, dropped when I left the function, but the timer remained to be triggered. Oy. Works great now though.