html - JavaScript Clock -
i ran snag in code, code below javascript clock works perfectly:
function rendertime() { var currenttime = new date(); var diem = "am"; var h = currenttime.gethours(); var m = currenttime.getminutes(); var s = currenttime.getseconds(); if(h == 0) { h = 12; } else if(h > 12) { h = h - 12; diem = "pm"; } if(h < 10) { h = "0" + h; } if(m < 10) { m = "0" + m; } if(s < 10) { s = "0" + s; } var myclock = document.getelementbyid('clockdisplay'); myclock.textcontent = h + ":" + m + ":" + s + " " + diem; myclock.innerhtml = h + ":" + m + ":" + s + " " + diem; myclock.innertext = h + ":" + m + ":" + s + " " + diem; settimeout('rendertime()',1000); } rendertime();
however trying different this:
function maketime() { var currenttime = new date(); var diem = "am"; var h = currenttime.gethours(); var m = currenttime.getminutes(); var s = currenttime.getseconds(); if(h == 0) { h = 12; } else if(h > 12) { h = h - 12; diem = "pm"; } if(h < 10) { h = "0" + h; } if(m < 10) { m = "0" + m; } if(s < 10) { s = "0" + s; } var clock = document.getelementbyid('clock'); clock.innerhtml = "<h1>"+h+":"+m+":"+s+":"+diem+"</h1>"; myclock.textcontent = "<h1>"+h+":"+m+":"+s+":"+diem+"</h1>"; myclock.innertext = "<h1>"+h+":"+m+":"+s+":"+diem+"</h1>"; settimeout('maketime()',1000); } maketime();
this 1 works, not update other one, have manually refresh page.
what doing wrong?
you continue refer myclock
in second version, when you've renamed variable clock
:
var clock = ... clock.innerhtml = ... myclock.textcontent = "<h1>"+h+":"+m+":"+s+":"+diem+"</h1>"; myclock.innertext = "<h1>"+h+":"+m+":"+s+":"+diem+"</h1>"; settimeout('maketime()',1000);
this causing errors (reference error: myclock not defined
) preventing flow of execution reaching settimeout
call.
you should learn use tools available you. every browser has method of reporting javascript errors developers. open developer console in webkit/ie10, or firebug in firefox, you'll see these errors , they're happening in code.
Comments
Post a Comment