结果显示为什么是标准时间格式和时分秒格式交替出现?

来源:8-3 计时器setInterval()

phoenix_0010

2018-08-04 17:27


<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;

  }

 var int=setInterval(clock,2000) ;

</script>

</head>

<body>

<form>

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

</form>

</body>

</html>

结果显示,先是显示:Sat Aug 04 2018 17:27:19 GMT+0800 (中国标准时间)

再显示:17:27:19

写回答 关注

5回答

  • qq_夏佐_0
    2018-11-28 18:32:48

    我的代码:这样也可以:setInterval("clock()",100);


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

      

    </script>

    </head>

    <body>

    <form>

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

    </form>

    </body>

    </html>


  • qq_夏佐_0
    2018-11-28 18:24:29

    我的代码:


    <!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,100);

      

    </script>

    </head>

    <body>

    <form>

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

    </form>

    </body>

    </html>


  • _阿困君_
    2018-10-08 14:23:26

    浏览器刷新后重新提交代码

  • 慕虎144582
    2018-09-27 14:27:11

    <script type="text/javascript">

      var attime;

      function clock(){

        var time=new Date();          

        var h = time.getHours();

        var i = time.getMinutes();

        var s = time.getSeconds();

        var attime = check(h)+":"+check(i)+":"+check(s);

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

      }

      

      function check(s){

        if(s<10){

            return "0"+s;

        }else{

            return s;

        }

      }

      setInterval("clock()",1000);

      

    </script>


  • 沐沐26
    2018-08-05 20:02:25

    我把你的代码粘贴到我本地运行只显示时分秒格式

    qq_med...

    请问你的本地编译器使用什么的啊?

    2018-10-07 09:50:30

    共 2 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468060 学习 · 21891 问题

查看课程

相似问题