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

怎么弄停止后继续,继续后还能停止

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>计时器</title>

<script type="text/javascript">

function clock(){

var time = new Date();

document.getElementById("clock").value = time;

}

var i = setInterval(clock,100);

</script>

</head>

<body>

  <form>

    <input type="text" id="clock" size="50"  />

    <input type="button" value="Stop" onclick="clearInterval(i)"/>

    <input type="button" value="continue" onclick="setInterval(i)"">

  </form>

</body>

</html>


提问者:fantme 2019-09-22 10:16

个回答

  • 阳火锅
    2019-09-24 16:37:09
    已采纳

    http://img4.mukewang.com/5d89d5960001359607300091.jpg把这个替换成

    <input type="button" value="continue" onclick="setInterval(clock,100);">

  • qq_慕慕947699
    2019-10-09 15:32:14

    <!DOCTYPE HTML><html><head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>计时器(暂停/开始)</title>  
    <script type="text/javascript">    
    var timer = null;    
    ooop();    
    function ooop() {      
        if (!timer) {
            timer = setInterval( function clock(){          
                var time = new Date();          
                document.getElementById("clock").value = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds() ;;        
            }, 1000);      
         } else {        
             clearInterval(timer)        
             timer = null;      
         }    
     }  
     </script>
     </head>
     <body>  
     <form>    
     <input type="text" id="clock" size="50" />    
     <br />    
     <input type="button" value="StopOrOpen" onclick="ooop()" />  
     </form>
     </body>
     </html>