问答详情
源自:8-4 取消计时器clearInterval()

结束不好使了 什么情况

<!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>


提问者:qq_慕九州8416717 2020-01-15 15:00

个回答

  • 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>