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>