Home >Web Front-end >JS Tutorial >How to delete delayer in JavaScript
How to delete a delayer in JavaScript: 1. Use the "clearInterval(id)" statement to delete the delayer defined by setInterval(); 2. Use the "clearTimeout(id)" statement to delete it Delay defined by setTimeout().
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript delayer, also known as timer, sometimes also called "timer", is used to perform certain tasks after a specified time, similar to the alarm clock in our lives.
In JavaScript, we can use delayers to delay the execution of certain codes, or to repeatedly execute certain codes at fixed intervals.
JavaScript provides two ways to set the timer, namely setTimeout() and setInterval(); and there are two corresponding ways to delete the delayer:
clearInterval() Cancel the timeout set by setInterval().
clearTimeout() Cancel the timeout set by the setTimeout() method.
clearInterval()
clearInterval() method can cancel the timeout set by setInterval().
Syntax:
clearInterval(id)
The parameter of the clearInterval() method must be the ID value returned by setInterval().
Example:
<html> <body> <input type="text" id="clock" size="35" /> <script language=javascript> var int=self.setInterval("clock()",50) function clock() { var t=new Date() document.getElementById("clock").value=t } </script> </form> <button onclick="int=window.clearInterval(int)"> Stop interval</button> </body> </html>
clearTimeout()
The clearTimeout() method can cancel the timeout set by the setTimeout() method.
Syntax:
clearTimeout(id)
The parameter of the clearInterval() method must be the ID value returned by setTimeout().
Example:
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } function stopCount() { clearTimeout(t) } </script> </head> <body> <form> <input type="button" value="Start count!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="Stop count!" onClick="stopCount()"> </form> </body> </html>
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to delete delayer in JavaScript. For more information, please follow other related articles on the PHP Chinese website!