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

比较完整地答案

<script type="text/javascript">

  setInterval(clock,1000);

  var attime;

  function clock(){

    var time=new Date();

    var hour = time.getHours()<10?"0"+time.getHours():time.getHours();

    var minute = time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes();

    var second = time.getSeconds()<10?"0"+time.getSeconds():time.getSeconds();

    attime= hour+"时:"+minute+"分:"+second+"秒";

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

  }

</script>


提问者:慕粉1474278593 2018-12-19 10:15

个回答

  • qq_慕妹1468219
    2018-12-25 16:07:55

    明白了谢谢


  • 慕粉1474278593
    2018-12-24 00:05:54

    比如早上九点零五分,你可以写成标准的09:05,保证都是两位数

  • qq_慕妹1468219
    2018-12-23 09:07:03

     time.getHours()<10?"0"+time.getHours():time.getHours();

    请问这小于10是什么意思啊

  • lol388
    2018-12-21 11:09:17

    <!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();          

        attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();

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

      }

     setInterval(clock,1000); 

    </script>

    </head>

    <body>

    <form>

    <input type="text" id="clock" size="50"  style="background:#000;color:#00ff00;width:50px"; />

    </form>

    </body>

    </html>