Jump to content

Time


amalosoul

Recommended Posts

Why doesn't this update the time without my having to refresh the page?

function startTime(hour,mins,sec)
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime(hour,mins,sec)',500)
}
//then I call this function when the page loads, and add the div element

Thank you!
Link to comment
https://forums.phpfreaks.com/topic/29926-time/
Share on other sites

Well you are going to have multiple instances of the timeout if you don't use clearInterval. Personally I would create another function for the updates.. Here is an example.

[code]
function MyFunction(hour, mins, sec) {
  clearInterval(t);
  startTimer(hour, mins, sec);
}[/code]

Now just change the function called in your setTimeout to Myfunction.

The reason that I place the clearInterval outside of the startTimer function is that it may cause an error when the page first loads because the variable t is not an interval.

Good Luck,
Tom
Link to comment
https://forums.phpfreaks.com/topic/29926-time/#findComment-137577
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.