结束不好使了 什么情况

来源:8-4 取消计时器clearInterval()

qq_慕九州8416717

2020-01-15 15:00

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>定时器</title>
    <script type="text/javascript">
        var i;
        function start(){
            var time=new Date();
            var attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
            document.getElementById('clock').value = attime;
            i = setInterval(start,1000);
        }
        function stop() {
            clearInterval(i);
        }
    </script>
</head>
<body>
<form>
    <input type="text" id="clock" size="50"  />
    <input type="button" value="Start" onclick="start()">
    <input type="button" value = "Stop" onclick = "stop()" />
</form>
</body>
</html>


写回答 关注

2回答

  • little_bee
    2020-02-22 18:27:54

    定时器移出start函数

  • 晓之蛇
    2020-01-15 16:13:45
    <!DOCTYPE HTML>
    <html>
    <head>    
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    
        <title>定时器</title>    
        <script type="text/javascript">        
            var i;        
            function start(){            
                var time=new Date();            
                var attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
                document.getElementById('clock').value = attime;                
            }        
            i = setInterval(start,1000);    
        </script>
    </head>
    <body>
        <form>    
            <input type="text" id="clock" size="50"  />    
            <input type="button" value="Start" onclick="setInterval(start,1000);">    
        <    input type="button" value = "Stop" onclick = "clearInterval(i)" />
        </form>
    </body>
    </html>


JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468061 学习 · 21891 问题

查看课程

相似问题