Home >Web Front-end >JS Tutorial >Simple JS clock example explanation_javascript skills
The example in this article introduces the JS clock implementation code and shares it with you for your reference. The specific content is as follows
Rendering:
Specific code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="gb2312"> <title>Document</title> <script type="text/javascript"> function startTime () { var today=new Date(); var y=today.getFullYear(); var mon=today.getMonth()+1; mon=changNum(mon); var d=today.getDay(); d=changNum(d); var h=today.getHours(); var m=today.getMinutes(); m=changNum(m); var s=today.getSeconds(); s=changNum(s); document.getElementById("timeid").innerHTML=y+"年"+mon+"月"+d+"日"+h+":"+m+":"+s; t=setTimeout(function(){ startTime(); },500); } function changNum(i){ //月、日、秒如果小于10数字前加0 if(i<10){ return "0"+i; } return i; } </script> </head> <body onload="startTime()"> <h1 id="timeid"></h1> </body> </html>
I hope this article will be helpful to everyone learning JavaScript programming.