?????? ??? ???
JavaScript ??? ???
JavaScript? ???? ??? ??? ??? ??? ??? ?? ?? ?? ??? ??? ? ????. ??? ??? ??? ????? ????.
? ??? ??? ???? 5? ?? ?? ??? ?????.
<html> <head> <script type="text/javascript"> function timedMsg() { var t=setTimeout("alert('5 秒!')",5000) } </script> </head> <body> <form> <input type="button" value="顯示定時(shí)的警告框" onClick = "timedMsg()"> </form> <p>請(qǐng)點(diǎn)擊上面的按鈕。警告框會(huì)在 5 秒后顯示。</p> </body> </html>
? ?? ????? 2?, 4?, 6?? ???? ?????.
<html> <head> <script type="text/javascript"> function timedText() { var t1=setTimeout("document.getElementById('txt').value='2 秒'",2000) var t2=setTimeout("document.getElementById('txt').value='4 秒'",4000) var t3=setTimeout("document.getElementById('txt').value='6 秒'",6000) } </script> </head> <body> <form> <input type="button" value="顯示計(jì)時(shí)的文本" onClick="timedText()"> <input type="text" id="txt"> </form> <p>點(diǎn)擊上面的按鈕。輸入框會(huì)顯示出已經(jīng)逝去的時(shí)間(2、4、6 秒)。</p> </body> </html>
? ???? ??? ? Start Timing ??? ??? ????? 0?? ? ??? ???? ?????
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } </script> </head> <body> <form> <input type="button" value="開始計(jì)時(shí)!" onClick="timedCount()"> <input type="text" id="txt"> </form> <p>請(qǐng)點(diǎn)擊上面的按鈕。輸入框會(huì)從 0 開始一直進(jìn)行計(jì)時(shí)。</p> </body> </html>
? ???? Count ??? ??? ? ???? ??? ?? ???? ?????? ???? Stop ??? ???? ???? ?????
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } function stopCount() { c=0; setTimeout("document.getElementById('txt').value=0",0); clearTimeout(t); } </script> </head> <body> <form> <input type="button" value="開始計(jì)時(shí)!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="停止計(jì)時(shí)!" onClick="stopCount()"> </form> <p>請(qǐng)點(diǎn)擊上面的“開始計(jì)時(shí)”按鈕來啟動(dòng)計(jì)時(shí)器。輸入框會(huì)一直進(jìn)行計(jì)時(shí),從 0 開始。點(diǎn)擊“停止計(jì)時(shí)”按鈕可以終止計(jì)時(shí),并將計(jì)數(shù)重置為 0。</p> </body> </html>
A ?? JavaScript ??:
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) } function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </head> <body onload="startTime()"> <div id="txt"></div> </body> </html>
JavaScript Timing Events
JavaScript? ???? ??? ??? ??? ??? ??? ?? ?? ?? ??? ??? ? ????. ??? ??? ??? ????? ????.
JavaScript?? ??? ???? ???? ?? ?? ????. ? ?? ?? ???? ??? ????.
setTimeout() ??? ?? ??clearTimeout() setTimeout() ??
setTimeout()
Syntax
var t= setTimeout("javascript ?", ???)
setTimeout() ???? ?? ?? ?????. ?? ????? ?? t?? ??? ?????. ? setTimeout()? ????? ? ?? ????? ???? ??? ? ????.
setTimeout()? ? ?? ????? JavaScript ?? ??? ??????. ?? "alert('5 second!')"? ?? ?? ?? ??, AlertMsg()"? ?? ??? ?? ??? ?? ????.
? ?? ?? ??? ? ?? ?? ??? ??? ?? ?????? ? ???? ????? ?????.
?: 1000???? 1?? ????.
??? ???? ??? ??????
clearInterval() ???? setInterval() ??? ??? ???? ? ?????. (intervalVariable)
window .clearInterval() ???? ? ??? ? ???? ?? ?????clearInterval() ??? ??? ? ????. clearInterval() ???? ????? ??? ???? ??? ? ?? ??? ???? ???. myVar=setInterval("javascript function",milliseconds); ?? ?? ClearInterval() ???? ???? ??? ??? ? ????. <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>時(shí)鐘停止示例</title>
</head>
<body>
<p>頁面上顯示時(shí)鐘:</p>
<p id="demo"></p>
<button onclick="myStopFunction()">停止時(shí)鐘</button>
<script>
var myVar=setInterval(function(){myTimer()},1000);
function myTimer(){
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function myStopFunction(){
clearInterval(myVar);
}
</script>
</body>
</html>