Home  >  Article  >  Web Front-end  >  How to stop javascript timer

How to stop javascript timer

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-15 14:39:5012548browse

In JavaScript, you can use the clearInterval method to stop the timer. The syntax is "clearInterval (timeout set by the setInterval method)". The parameter of this method must be the ID value returned by setInterval.

How to stop javascript timer

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
    #btn1:hover,#btn2:hover {
        background: red;
    }
</style>
</head>

<body>
<input type="button" id="btn1" value="开启" />
<input type="button" id="btn2" value="关闭" />
<script>
    var obtn1=document.getElementById(&#39;btn1&#39;);
    var obtn2=document.getElementById(&#39;btn2&#39;);
    var timer=null;
    obtn1.onclick=function()
    {
        timer=setInterval(function()   //开启循环:每秒出现一次提示框
        {
            alert(&#39;a&#39;);
        },1000);
    }
    obtn2.onclick=function()
    {
        clearInterval(timer);        //关闭循环
    }
</script>
</body>
</html>

clearInterval() method can cancel the timeout set by setInterval().

The parameter of the clearInterval() method must be the ID value returned by setInterval().

Grammar

clearInterval(id_of_setinterval)

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to stop javascript timer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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