简体   繁体   English

如何使计时器仅在单击开始按钮时开始

[英]How to make timer begin ONLY when Start Button is clicked

My timer starts when page is opened versus when clicking the Start button.我的计时器在页面打开时与单击“开始”按钮时启动。 I have spent hours trying to resolve the problem based on the suggestions I read in this forum: Make countdown start after button is clicked .我花了几个小时试图根据我在本论坛中阅读的建议解决问题: 单击按钮后开始倒计时 Thank you in advance for your assistance.预先感谢您的帮助。

My Code:我的代码:

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;
}

You need to remove the call to timer() :您需要删除对timer()的调用:

function timer() {
    t = setTimeout(add, 1000);
}
//timer();//this causes the timer to start one the page is loaded

Also there is a missing brace } in your code here:您的代码中还有一个缺少大括号}

if (seconds >= 60) {
    seconds = 0;
    minutes++;
    //missing } here

See working snippet查看工作片段

 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 = function(){ start.disabled = true; timer(); }; /* Stop button */ stop.onclick = function() { clearTimeout(t); start.disabled = false; } /* Clear button */ clear.onclick = function() { h1.textContent = "00:00:00"; seconds = 0; minutes = 0; hours = 0; }
 <h1></h1> <button id="start">Start</button> <button id="stop">Stop</button> <button id="clear">Clear</button>

Implementation using setInterval (this way you can avoid the recursive call to timer() in your add function):使用setInterval实现(这样可以避免在add函数中递归调用timer() ):

 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); } function timer() { t = setInterval(add, 1000); } //timer(); /* Start button */ start.onclick = function(){ start.disabled = true; timer(); }; /* Stop button */ stop.onclick = function() { clearInterval(t) start.disabled = false; } /* Clear button */ clear.onclick = function() { h1.textContent = "00:00:00"; seconds = 0; minutes = 0; hours = 0; }
 <h1></h1> <button id="start">Start</button> <button id="stop">Stop</button> <button id="clear">Clear</button>

Remove timer() underneath function timer() { .删除timer()下方function timer() { If that doesn't help, try removing timer() in add() as well.如果这没有帮助,也尝试在add()删除timer()

You call timer() in your code, before the start button is pressed.在按下开始按钮之前,您在代码中调用timer()

timer();

Remove that line from your code and test it;从您的代码中删除该行并进行测试; the timer shouldn't begin on page load once you do that.一旦你这样做,计时器就不应该在页面加载时开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM