如何在Javascript中自动更新时钟?

时间:2016-11-15 11:53:21

标签: javascript

我正在尝试使用纯java脚本制作一个简单的基于Web的时钟应用程序。我认为通用代码是正确的,但我不确定如何在设定的时间间隔内自动调用该函数。我认为window.onload,然后是setInterval方法会这样做。但它不像我预期的那样每半秒自动更新一次。我做错了什么?

<!doctype html>

<html>

    <head>
        <title>Real Time Clock</title>
        <script>

            var time, h, m, s, track;
            track = 0;
            window.onload = function() { setInterval( timeNow(), 100); }

            function timeNow() {

              time = new Date();
              track += 1;
              h = time.getHours();
              m = time.getMinutes();
              s = time.getSeconds();
              if ( s < 10 ) { s = "0" + s; } /* we add a 0 in front of s, when it is lower than 10, because that's what most clocks display, this is for the human user rather than for any need by the computer */
              document.getElementById("time").innerHTML = h + ':' + m + ':' + s;
              document.getElementById("track").innerHTML = track;

            }

        </script>
    </head>

    <body>

        <span id="time">~Waiting for time update.</span><br>
        <span id="track"></span>


    </body>

</html>

3 个答案:

答案 0 :(得分:0)

似乎工作正常。只需改变

window.onload = function() { setInterval( timeNow(), 100); }

 window.onload = function() { setInterval( timeNow, 100); }

&#13;
&#13;
<!doctype html>

<html>

    <head>
        <title>Real Time Clock</title>
        <script>

            var time, h, m, s, track;
            track = 0;
            window.onload = function() { setInterval( timeNow, 100); }

            function timeNow() {

              time = new Date();
              track += 1;
              h = time.getHours();
              m = time.getMinutes();
              s = time.getSeconds();
              if ( s < 10 ) { s = "0" + s; } /* we add a 0 in front of s, when it is lower than 10, because that's what most clocks display, this is for the human user rather than for any need by the computer */
              document.getElementById("time").innerHTML = h + ':' + m + ':' + s;
              document.getElementById("track").innerHTML = track;

            }

        </script>
    </head>

    <body>

        <span id="time">~Waiting for time update.</span><br>
        <span id="track"></span>


    </body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用setInterval(timeNow,100);代替setInterval(timeNow(),100);

&#13;
&#13;
            var time, h, m, s, track;
            track = 0;
            window.onload = function() { 
              var start = setInterval(timeNow,100);
            }

            function timeNow() {

              var time = new Date();
              track += 1;
              h = time.getHours();
              m = time.getMinutes();
              s = time.getSeconds();
      
              if ( s < 10 ) { s = "0" + s; } 
              document.getElementById("time").innerHTML = h + ':' + m + ':' + s;
              document.getElementById("track").innerHTML = track;

            }
&#13;
     

        <span id="time">~Waiting for time update.</span><br>
        <span id="track"></span>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您正在将undefined(作为调用timeNow的结果)传递给setInterval。您需要传递函数,因此代码将是:

window.onload = function() { setInterval(timeNow, 100); }