0

I am trying to make a simple web based clock app using pure java-script. I think the general code is right, but I'm not sure how to automatically call the function at a set interval of time. I thought the window.onload, followed by the setInterval method would do this. But it's not automatically updating every half second as I expected. What am I doing wrong?

<!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>
Daniel Foreman
  • 105
  • 2
  • 6

3 Answers3

0

Seems to work fine. Just needed to change

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

to

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

<!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>
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
0

use setInterval(timeNow,100); instead of setInterval(timeNow(),100);

            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;

            }
     

        <span id="time">~Waiting for time update.</span><br>
        <span id="track"></span>
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

You are passing an undefined (as the result of calling timeNow) to setInterval. You need to pass the function, so the code will be:

window.onload = function() { setInterval(timeNow, 100); }
srifqi
  • 71
  • 1
  • 6