osiris1@yahoo.com
Posts: 0
Joined: Mon Jun 10, 2013 5:34 pm

Update label every second with Javascript

Hello,

I am attempting to build an online clock using javascript. How do I get the display (in my case I'm using a label control) to update every second?

Here is my code that i have on the LOAD event for the page

var todayDate=new Date();
var hours=todayDate.getHours();
var minutes=todayDate.getMinutes();
var seconds=todayDate.getSeconds();
var format ="AM";
if(hours11)
{format="PM";
}
if (hours 12) { hours = hours - 12; }
if (hours == 0) { hours = 12; }
if (minutes < 10){
minutes = "0" + minutes;
}
var outclock = hours + ":" + minutes + ":" + seconds + " " +format;

Appery("ClockFace").text(outclock);

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Update label every second with Javascript

Add a JavaScript timer to invoke your function every x-seconds.

osiris1@yahoo.com
Posts: 0
Joined: Mon Jun 10, 2013 5:34 pm

Update label every second with Javascript

how? and what event would the code be placed in. Right now I have it in LOAD.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Update label every second with Javascript

setTimeout(...)

https://developer.mozilla.org/en-US/d...

You would use Load event to load the setTimeout function and from there it would invoke the code that you need.

osiris1@yahoo.com
Posts: 0
Joined: Mon Jun 10, 2013 5:34 pm

Update label every second with Javascript

Opps. I got it now.

setInterval(function(){myTimer()},1000); and I left the code in the LOAD event and it works!!!

Return to “Issues”