Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q590 How do I check for the existence of a setTimeout() and remove it?

You are here: irt.org | FAQ | JavaScript | Timeout | Q590 [ previous next ]

Use a boolean flag to indicate when the timer is running. Retain the timer identity, and use it to cancel the timer:

<script language="JavaScript"><!--
var timerRunning = false; // boolean flag
var myTimer = null;

function myFunction() {
    // whenever the timer 'awakes' set the flag to false:
    timerRunning = false;
    // insert your code here...
}

function startTimer() {
    myTimer = setTimeout=('myFunction()',10000); // myTimer holds the id of the timer
    timerRunning = true; // whenever you start a timer set the timerRunning flag to true
}

function stopTimer() {
    if (timerRunning)
        clearTimeout(myTimer);
}
//--></script>

<form>
<input type="button" value="Start" onClick="startTimer()">
<input type="button" value="Start" onClick="stopTimer()">
</form>

Feedback on 'Q590 How do I check for the existence of a setTimeout() and remove it?'

©2018 Martin Webb