我正在嘗試使以下JavaScript計時器僅通過單擊一個按鈕來執行兩個功能 - 單擊定時器啓動一次;再次點擊;它停止。再次點擊第三次,等等。我在這裏做錯了什麼?非常感謝你提前。javascript定時器
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
function both(){
doTimer();
stopCount();
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onclick="doTimer" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onclick="stopCount()" />
</form>
<p>
Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
</p>
</body>
</html>
你應該格式化你的代碼的代碼。在每個代碼行前添加四個空格。 – jpsimons 2011-04-23 21:25:52
我在那裏看到兩個按鈕。與你的問題相關的代碼在哪裏? – 2011-04-23 21:26:01
@query:人們已經回答了你的問題,請花點時間,並將你認爲是你問題的最佳答案的標記,問候 – 2013-05-13 11:04:29