javascript - how to update time regularly? -


function timeclock() {     settimeout("timeclock()", 1000);             = new date();     alert(now);     f_date = now.getdate()+" "+strmonth(now.getmonth())+" "+now.getfullyear()+" / "+timeformat(now.gethours(), now.getminutes());     return f_date; }  <span class="foo"><script type="text/javascript">document.write(timeclock());</script></span> 

alert(now); gives me value every second not updated in html. how can update time on html without refresh page?

there number of mistakes in code. without use of var infront of variable declarations, leak them global scope.

also, use of document.write discouraged.

here's how it:

javascript:

function updateclock() {     var = new date(), // current date         months = ['january', 'february', '...']; // idea         time = now.gethours() + ':' + now.getminutes(), // again, idea          // cleaner way string concatenation         date = [now.getdate(),                  months[now.getmonth()],                 now.getfullyear()].join(' ');      // set content of element id time formatted string     document.getelementbyid('time').innerhtml = [date, time].join(' / ');      // call function again in 1000ms     settimeout(updateclock, 1000); } updateclock(); // initial call 

html:

<div id="time"> </div> 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -