gpt4 book ai didi

Javascript 秒表自动启动

转载 作者:行者123 更新时间:2023-12-02 16:12:46 26 4
gpt4 key购买 nike

我的页面上有一个 Javascript/HTML 秒表,但每当页面加载时它总是自动启动。我不懂 Java,有人可以调整代码,使其仅在按下“开始”按钮时启动吗?

var h1 = document.getElementsByTagName('h1')[0],
start = document.getElementById('start'),
stop = document.getElementById('stop'),
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;

function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}

h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);

timer();
}
function timer() {
t = setTimeout(add, 1000);
}
timer();


/* Start button */
start.onclick = timer;

/* Stop button */
stop.onclick = function() {
clearTimeout(t);
}

/* Clear button */
clear.onclick = function() {
h1.textContent = "00:00:00";
seconds = 0; minutes = 0; hours = 0;
}

最佳答案

您在函数定义后调用 timer()。删除该行:

function timer() {
t = setTimeout(add, 1000);
}
// timer(); // comment this line

关于Javascript 秒表自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com