Home >Web Front-end >JS Tutorial >Definition and usage of js clearInterval() method_javascript skills
This method can cancel the timer set by the setInterval() method .
The parameter of this method must be the return value of the corresponding setInerval() method to be canceled.
Click to see more properties and methods of the window object.
Grammar structure:
clearInterval(id)
Parameter list:
Browser support:
(1). IE browser supports this attribute.
(2).Firefox browser supports this attribute.
(3). Opera browser supports this attribute.
(4). Chrome browser supports this attribute.
(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对象的clearInterval()方法 -蚂蚁部落</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">点击取消</button></div> </body> </html>
Click the button with the above code to cancel the digital auto-increment effect.