1 <html> 2 <head> 3 <title>定時刷新時間</title> 4 <script language="JavaScript"> //<script language="JavaScript" src="js.js"> 5 function time(){ 6 var now =new Date().toLocaleString(); 7 alert(now); 8 } 9 10 function start(){ 11 timmerID=window.setInterval("time()",1000); //調取函數,調取頻率毫秒 12 } 13 function stop(){ 14 window.clearInterval(timmerID); //只有遇到clearInterval() 才中止 15 } 16 17 </script> 18 </head> 19 <body> 20 <input type ="button" value="時間" onclick="time()"></button> 21 <input type ="button" value="啓動" onclick="start()"></button> 22 <input type ="button" value="中止" onclick="stop()"></button> 23 24 25 </body> 26 </html>
1 <html> 2 <head> 3 <title>時鐘、倒計時</title> 4 <script language="JavaScript"> 5 6 function rtime(){ 7 var djs = document.getElementById("count").innerText; 8 if(djs!=0){ 9 document.getElementById("count").innerText=djs-1; 10 setTimeout("rtime()",100); //調取函數,調取頻率毫秒 只掉一次 11 } 12 } 13 function time(){ 14 var now = new Date(); 15 var hour = now.getHours(); 16 var min = now.getMinutes(); 17 var second = now.getSeconds(); 18 var apm = "AM"; 19 if(hour>12){ 20 hour=hour-12; 21 apm = "PM"; 22 } 23 if(hour<10){ 24 hour="0"+hour; 25 } 26 if(min<10){ 27 min="0"+min; 28 } 29 if(second<10){ 30 second="0"+second; 31 } 32 document.form1.myclock.value = hour+":"+min+":"+second+" "+apm ; 33 setTimeout("time()",1000); 34 } 35 36 </script> 37 </head> 38 <body onload="time()"> 39 <form name="form1"> 40 <input type="text" name="myclock"> 41 <div id="count" >111</div> 42 <input type="button" value="倒計時開始" onclick="rtime()"> 43 </form> 44 45 </body> 46 </html>