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

我并没有点start 只要运行就会增加 而且点stop也没有用

<!DOCTYPE HTML>

<html>

<head>

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

<title>计时器</title>


<script type="text/javascript">

  var num=0;

  var i;

  function startCount(){

    document.getElementById('count').value=num;

    num=num+1;

    i=setTimeout("startCount()",1000);

  }

  setTimeout("startCount()",1000);

  function stopCount(){

  clearTimeout(i);

  }

</script>

</head>

<body>

  <form>

    <input type="text" id="count" />

    <input type="button" value="Start"  Click="startCount"/>

    <input type="button" value="Stop"  Click="stopCount" />

  </form>

</body>

</html>


提问者:努力努力再努力圈 2020-02-22 12:06

个回答

  • 慕尼黑1441384
    2020-08-02 21:21:13

    成功持行的代码:

    <!DOCTYPE HTML>

    <html>

    <head>

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

    <title>计时器</title>


    <script type="text/javascript">

      var num=0;

      var i;

      function startCount(){

        document.getElementById('count').value=num;

        num=num+1;

        i=setTimeout("startCount()",1000);

      }

      function stopCount(){

        clearTimeout(i);

      }

    </script>

    </head>

    <body>

      <form>

        <input type="text" id="count" />

        <input type="button" value="Start" onclick="startCount()" />

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

      </form>

    </body>

    </html>


  • 慕村6055097
    2020-04-20 10:46:25

    <!DOCTYPE HTML>

    <html>

    <head>

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

    <title>计时器</title>


    <script type="text/javascript">

      var num=0;

      var i;

      function startCount(){

        document.getElementById('count').value=num;

        num=num+1;

        i=setTimeout("startCount()",1000);

      }

      function stopCount(i){

        clearTimeout(i);

      }

    </script>

    </head>

    <body>

      <form>

        <input type="text" id="count" />

        <input type="button" value="Start" onclick="startCount()" />

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

      </form>

    </body>

    </html>


  • 流星64
    2020-03-13 13:26:03

    你点击事件都写错了,计时器多写了一个

  • 朦胧5
    2020-02-23 16:19:07

    你startCount()函数下面有一条触发语句     setTimeout("startCount()",1000);