|
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?'
|
|
Copyright © 1996-2008 irt.org, All Rights Reserved.