下面我们就让它自动刷新,就得要用到它的setTimeout函数了,//setInterval也行不过是有区别的哦。。。。
View Code
1 function Clock() 2 { 3 var date = new Date(); 4 var year = date.getFullYear(); 5 var month = date.getMonth(); 6 var day = date.getDate(); 7 var week = date.getDay(); 8 var hour = date.getHours(); 9 var minute = date.getMinutes(); 10 var second = date.getSeconds(); 11 var millisecond = date.getMilliseconds(); 12 var flag = "AM" ; 13 if( hour < 10){ 14 hour = "0"+hour; 15 } 16 if(hour > 12){ 17 flag = "PM"; 18 } 19 if(minute <10){ 20 minute = "0" + minute; 21 } 22 if(millisecond < 10){ 23 millisecond = "0" + millisecond; 24 } 25 switch(week){ 26 case 1:week = "一"; break; 27 case 2:week = "二"; break; 28 case 3:week = "三"; break; 29 case 4:week = "四"; break; 30 case 5:week = "五"; break; 31 case 6:week = "六"; break; 32 default: week = "日";break; 33 } 34 var result = year+"年 "+month+"月 "+day+"日 星期"+week+" "+hour+":"+minute+":"+second + " "+flag; 35 document.getElementById("time").innerHTML=result; 36 //有可能会只显示一会就停止了 37 //setInterval("Clock()",1000); 38 setTimeout("Clock()",1000); 39 } 40 41 </script> 42 <style type="text/css"> 43 </style> 44 </head> 45 46 <body onload = "Clock()"> 47 <div id = "time"></div>