Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Timeout | Q590 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.