问答详情
源自:8-3 计时器setInterval()

没反应,求救

<!DOCTYPE HTML>

<html>

<head>

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

<title>定时器</title>

<script type="text/javascript">

  var attime;

  function clock(){

    var time=new Date();  

    var hour=time.getHours();

    var minute=time.getMinutes();

    var second=time.getSeconds();

    var date=document.write(hour + ":"+minute+":"+second);

   var attime=date;

   setInterval(clock,100);

 

   

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

  }

  

</script>

</head>

<body>

<form>

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

</form>

</body>

</html>


提问者:慕雪83909 2019-07-21 15:39

个回答

  • 慕九州3204222
    2019-09-02 17:11:27

    有三个问题: 第一个document.write本身是输出 类似一个功能 而你应该是想把时间赋值给date  所以应该是 var date=document.write(hour + ":"+minute+":"+second)  ; 第二个问题,源代码中这句var date=document.write(hour + ":"+minute+":"+second); 最后这个分号用的是中文分号; 第三个问题 计时器应该放在函数外面  修改后就能正常运行了:

    <!DOCTYPE HTML>


    <html>


    <head>


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


    <title>定时器</title>


    <script type="text/javascript">


      var attime;


      function clock(){


        var time=new Date();  


        var hour=time.getHours();


        var minute=time.getMinutes();


        var second=time.getSeconds();


        var date=(hour + ":"+minute+":"+second);


       var attime=date;


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


      }

    setInterval(clock,1000);

      


    </script>


    </head>


    <body>


    <form>


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


    </form>


    </body>


    </html>


  • 湘风楚雨
    2019-07-26 17:27:11

    没有var date = document.write(hour + ":" + minute + ":" + second) 这种写法吧

  • 吴量
    2019-07-21 16:01:08

    <script type="text/javascript">

    window.onload = function(){

    var attime;

    function clock() {

    var time = new Date();

    var hour = time.getHours();

    var minute = time.getMinutes();

    var second = time.getSeconds();

    //var date = document.write(hour + ":" + minute + ":" + second) ;

    var attime = hour + ":" + minute + ":" + second;

    document.getElementById("clocks").value = "";

    document.getElementById("clocks").value = attime;

    setInterval(clock,1000)

    }

    clock()

    }

    </script>


  • 慕雪83909
    2019-07-21 15:50:42

    没听懂,那是在input里调用吗

  • 吴量
    2019-07-21 15:43:51

    clock函数没调用,setInterval 那里有问题,注意一下