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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.