Use window.clearTimeout() to stop timer in JavaScript

Description

The following code shows how to use window.clearTimeout() to stop timer.

Example


<html>
<head>
<script language="JavaScript">
function showTime(){<!--from   w  w w  .j av a2s  .  c  o m-->
myTime = new Date();
myString = myTime.getHours() + ":" + myTime.getMinutes() + ":";
myString += myTime.getSeconds();
document.myForm.myText.value = myString;
}
function stopTime(){
window.clearTimeout(myTimeout);
}
var myTimeout = window.setTimeout("showTime()", 1000)
</script>
</head>
<body>
<form name="myForm">
<input type=TEXT size=20 value="" name="myText">
<input type=BUTTON value="Clear Timeout" onClick="stopTime()">
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use window.clearTimeout() to stop timer in JavaScript
Home »
  Javascript Tutorial »
    Event »
      Timer Event
Javascript Tutorial Timer Event
Clear the timer in JavaScript
Create a time out timer in JavaScript
Create text move animation with timer in Ja...
Fire Timer event in JavaScript
Set the timeout for the timer according to ...
Start a timer and cancel a timer in JavaScr...
Stop a time out timer in JavaScript
Use timer in JavaScript
Use window.clearInterval() to stop a timer ...
Use window.clearTimeout() to stop timer in ...