Definition and usage of js clearInterval of methods

  • 2020-10-31 21:37:22
  • OfStack

This method cancels the timer set by the setInterval() method.

The argument to this method must be to cancel the return value of the corresponding setInerval() method.

Click to see more properties and methods for window objects.

Grammatical structure:

clearInterval(id)

Parameter list:

参数 描述
id 必需。此id是setInerval()的返回值,是此setInerval()方法的唯1标识。

Browser support:

(1).IE browser supports this property.
(2).Firefox browser supports this attribute.
(3).Opera browser supports this attribute.
(4).chrome browser supports this property.
(5).safria browser supports this method.

Code example:


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.softwhy.com/" />
<title>window The object's clearInterval() methods  - The ant tribe </title>
<style type="text/css">
#num{
 width:100px;
 height:100px;
 text-align:center;
 line-height:100px;
 background-color:green;
 margin:50px auto 0px auto;
 color:red;
}
#btdiv{
 width:76px;
 height:76px;
 margin:0px auto;
}
</style>
<script type="text/javascript">
var a=0;
window.onload=function(){
 var num=document.getElementById("num");
 var bt=document.getElementById("bt");
 function jisuan(){
 num.innerHTML=a;
 a=a+1;
 }
 var flag=setInterval(jisuan,1000);
 bt.onclick=function(){
 clearInterval(flag);
 }
}
</script>
</head>
<body>
<div id="num"></div>
<div id="btdiv"><button id="bt"> Click cancel </button></div>
</body>
</html>

Click the button above to cancel the digital autoincrement effect.


Related articles: