-1
我一直在討論這個問題。每當我點擊「開始」不止一次,它就累加起來。如果您發現,如果我點擊「開始」兩次,在我的時鐘計數開始行動滑稽:我的秒錶很有趣 - Javascript
var h1 = document.getElementsByTagName('h1')[0],
start = document.getElementById('start'),
stop = document.getElementById('stop'),
clear = document.getElementById('clear'),
seconds = 0,
minutes = 0,
GG;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
}
}
h1.textContent =
(minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") +
":" + (seconds > 9 ? seconds : "0" + seconds);
timer();
}
function timer() {
GG = setTimeout(add, 1000);
}
timer();
start.onclick = timer;
stop.onclick = function() {
clearTimeout(GG);
}
clear.onclick = function() {
h1.textContent = "00:00";
seconds = 0;
minutes = 0;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="time-container">
<h1><time>00:00</time></h1>
<button id="start">start</button>
<button id="stop">stop</button>
<button id="clear">clear</button>
</div>
</body>
</html>
我必須按STOP,N倍之多START。 如果我想解決這個問題,使任何後續的按下開始將無關緊要。隨時只會執行一次計數執行。
設置一個標誌...標誌=真; ...裏面你本功能>如果(標誌)做FUNC和設置標誌爲假,否則不做任何事情。 – Roy
澄清你的意思是「功能似乎增加了越來越多」。發生了什麼,你想要做什麼? – Aron
您發佈的代碼中沒有任何內容會導致該函數運行一次。你的代碼的其餘部分在哪裏? – Turnip