DeadJ
DeadJ

Reputation: 1

Issue with setInterval function call

I'm trying to call the setInterval function in my pop up window to update the time every second but when it is called my HTML page doesn't update at all just shows the initial time upon loading. I can't see what I'm doing wrong with this code.

    var currentTime = new Date();

window.self.setInterval(
    function()
    { 
        window.self.document.getElementById("Time").innerHTML = currentTime.toTimeString();
    }, 1000 );

Any reason why this is happening?

Upvotes: -1

Views: 112

Answers (1)

svidgen
svidgen

Reputation: 14302

currentTime is being set once and only once. You need to create a new Date object at every interval. Something like this:

setInterval(
  function()
  { 
    document.getElementById("Time").innerHTML = (new Date()).toTimeString();
  },
  1000
);

Upvotes: 3

Related Questions