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

请问为什么这个会循环,难道setTime()有循环功能

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0;
function numCount(){
document.getElementById('txt').value=num;
num=num+1;
setTimeout("numCount()",1000);
}
</script>
</head>
<body>
<form>
<input type="text" id="txt" />
<input type="button" value="Start" onClick="numCount()" />
</form>
</body>
</html>

提问者:qq_听一半的歌_1 2017-05-13 11:04

个回答

  • 慕勒7123956
    2017-05-13 13:22:12
    已采纳

    这个函数在自己的函数体中调用了自己,所以每过一秒都能执行一遍这个函数

  • qq_only哎you_0
    2017-05-31 16:48:15

    这叫递归


  • HOWEVERP
    2017-05-22 10:44:45

    //这个就不会

    <!DOCTYPE HTML>

    <html>

    <head>

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

    <title>计时器</title>

    <script type="text/javascript">

      var num=0;

      function startCount() {

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

        num=num+1;

        

      }

      setTimeout("startCount()",1000);

    </script>

    </head>

    <body>

    <form>

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

    <input type="button" value="click it" onclick="startCount()">

    </form>

    </body>

    </html>