i wanted make simple clock tells time , not date. problem code below though shows exact time of when .html file opened.
<body onload="showtime()"> <p id="time"</p> <script> var seconds; var minutes; var hours; var time; function showtime() { seconds = getseconds(); minutes = getminutes(); hours = gethours(); document.getelementbyid("demo").innerhtml = hours +":"+ minutes +":"+ seconds; }
is there anyway allow update automatically every second? thanks.
you need setinterval , proper date object, not want execute in body tag.
<!doctype html> <html> <head> <title>clock</title> <script> window.onload=function() { setinterval(showtime,500); // run every 500ms } function pad(num) { return ("0"+num).slice(-2); // pad number leading 0 } function showtime() { var d = new date(), // seconds = d.getseconds(); minutes = d.getminutes(); hours = d.gethours(); document.getelementbyid("demo").innerhtml = pad(hours) +":"+ pad(minutes) +":"+ pad(seconds); } </script> </head> <body> <p id="demo"></p> </body> </html>
Comments
Post a Comment