问答详情
源自:8-5 计时器setTimeout()

计数会跳数字

<title>计时器</title>
<script type="text/javascript">
  var num=0;
  function startCount() {
    document.getElementById('count').value=num;
    num ++;
    setTimeout(startCount,1000);
  }
  var count = setInterval(startCount,1000);
</script>
</head>
<body>
<form>
<input type="text" id="count" onclick = "startCount"/>



为甚么不是从0,1,2,3,这样计数,而是0,2,5,9这样计的,求指点

提问者:慕尼黑的女孩 2019-01-17 19:04

个回答

  • 毒素100
    2019-04-09 19:52:37

    第14行写这句: window.onload=startCount;

  • _Z6_
    2019-01-27 23:26:38

    <title>计时器</title>
    <script type="text/javascript">
      var num=0;
      function startCount() {
        document.getElementById('count').value=num;
        num ++;
        setTimeout("startCount()",1000);//修改处
      }
      var count = setTimeout("startCount()",1000);//修改处
    </script>
    </head>
    <body>
    <form>
    <input type="text" id="count" onclick = "startCount"/>


  • 逃离外包
    2019-01-25 16:48:42

    楼上回答应该没实践过,看下我的代码,我认为应该是onclick = "startCount",的问题,你可以自己再尝试下。

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <script type="text/javascript">

    var num=0;

    function startCount() {

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

    num ++;

    //   setTimeout(startCount,1000);

    setTimeout("startCount()",1000);

    }

    var count = setInterval(startCount,1000);

    </script>

    </head>

    <body>

    <input type="text" id="count" onclick="startCount()"/>

    </body>

    </html>


  • 慕仔1561325
    2019-01-17 20:59:38

    startCount()函数里面用了setTimeout,外面就不要再用setInterval,本来就无限循环计数了,你还要每隔一秒再运行一个无限循环计数,当然会跳,不但会跳,时间一长浏览器还会崩溃