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

关于定时器

为什么点一下按钮,文本框的数字就只加一;;而我要效果是点一下按钮,文本框内容就从0开始自动计数,请问哪里写错了??????

提问者:Ewall_ 2015-03-07 13:44

个回答

  • menghuanbaolei
    2015-03-07 14:13:17
    已采纳

    <!DOCTYPE HTML>

    <html>

    <head>

    <meta charset="utf-8">

    <title>计时器</title>

    </head>

    <script>

    window.onload=function ()

    {

        var oTxt1=document.getElementById("txt1");

        var oBtn1=document.getElementById("btn1");

        var num=0;

        

        oBtn1.onclick=function tt()

        {

             var time=setTimeout(function(){

              num++;

    oTxt1.value=num;

    setTimeout(tt(),0);

    },1000);

        }

        

    }

    </script>

    </head>

    <body>

    <form>

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

    <input id="btn1" type="button" value="点击" />

    </form>

    </body>

    </html>



  • menghuanbaolei
    2015-03-07 14:14:21

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>计时器</title>
    </head>
    <script>
    window.onload=function ()
    {
        var oTxt1=document.getElementById("txt1");
        var oBtn1=document.getElementById("btn1");
        var num=0;
        
        oBtn1.onclick=function tt()
        {
             var time=setTimeout(function(){
             	num++;
    oTxt1.value=num;
    setTimeout(tt(),0);
    },1000);
        }
    
        
    }
    </script>
    </head>
    <body>
    <form>
    <input id="txt1" type="text" />
    <input id="btn1" type="button" value="点击" />
    </form>
    </body>
    </html>