復制代碼 代碼如下:
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="開始計時!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止計時!" onClick="stopCount()">
</form>
<p>請點擊上面的“開始計時”按鈕來啟動計時器。輸入框會一直進行計時,從 0 開始。點擊“停止計時”按鈕可以終止計時。</p>
<p>timedCount(),1000ms自己調自己。</p>
</body>
</html>