Page 1 of 1

Update label every second with Javascript

Posted: Mon Sep 16, 2013 2:26 pm
by osiris1@yahoo.com

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);


Update label every second with Javascript

Posted: Mon Sep 16, 2013 2:28 pm
by maxkatz

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


Update label every second with Javascript

Posted: Mon Sep 16, 2013 2:39 pm
by osiris1@yahoo.com

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


Update label every second with Javascript

Posted: Mon Sep 16, 2013 2:41 pm
by maxkatz

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.


Update label every second with Javascript

Posted: Mon Sep 16, 2013 2:42 pm
by osiris1@yahoo.com

Opps. I got it now.

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