Home > Web Front-end > JS Tutorial > body text

How to delete delayer in JavaScript

青灯夜游
Release: 2021-10-27 18:24:26
Original
4700 people have browsed it

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().

How to delete delayer in JavaScript

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)
Copy after login

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>
Copy after login

clearTimeout()

The clearTimeout() method can cancel the timeout set by the setTimeout() method.

Syntax:

clearTimeout(id)
Copy after login

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(&#39;txt&#39;).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>
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template