This article describes the calling methods of setinterval() and clearInterval() JS functions. Share it for your reference. The details are as follows:
Copy the code as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Usage of setinterval() and clearInterval()</title>
<script type="text/javascript">
function f1() {
alert("Call f1");
}
function f2() {
alert("Call f2");
}
function bodymousedown() {
alert("Hello");
alert("I'm OK");
}
function com() {
if (confirm("whether to enter")) {
alert("entered");
}
else {
alert("Exit");
}
}
var interval;
function getinterval() {
if (confirm("Are you sure you want to execute?")) {
interval = setInterval("alert('Execute every 2000ms')", 2000);
}
else {
alert("not executed");
}
}
function setTimeOut1() {
setTimeout("alert('Execute this code after 3000ms')", 3000);
}
</script>
</head>
<!--" -->
<!--<body onmousedown ="bodymousedown()">-->
<body>
<!--ondblclick is a double-click event, onclick is a click event-->
<input type="button" onclick="document.ondblclick=f1" value="association event 1" />
<input type="button" onclick="document.ondblclick=f2" value="association event 2" />
<input type="button" ondblclick="bodymousedown()" value="call function" />
<input type="button" onclick="com()" value="usage of confirm" />
<input type="button" onclick="getinterval()" value="setInterval usage, execute the specified code every once in a while" />
<!--clearInterval cancels the timed execution of setInterval, which is equivalent to Enabled=False--> in Timer
<input type="button" onclick="clearInterval(interval)" value="Cancel the execution of setinterval code" />
<input type="button" onclick="setTimeOut1()" value="setTimeOut, code execution at a certain time" />
</body>
</html>
The setInterval() method calls a function or calculates an expression according to the specified period (in milliseconds).
The setInterval() method will call the function continuously until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as a parameter to the clearInterval() method.
grammar
Copy the code as follows: setInterval(code,millisec[,"lang"])
| parameter | describe |
|---|---|
| code | Required. The function to be called or the string of code to be executed. |
| millisec | must. The interval between periodic execution or calling code, in milliseconds. |
setTimeout is only run once, which means that after the set time is up, the specified code will be triggered to run, and it will end after the run is completed.
The setinterval runs in a loop, that is, the specified code is triggered every time the set time interval is reached. This is the real timer.
I hope this article will be helpful to everyone's JavaScript programming.