You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JS将秒表改造为从00:13:00开始到00:00:00停止的倒计时器

修改逻辑说明

  • 调整初始时间变量为13分钟,对应初始倒计时起点00:13:00
  • 将计时逻辑从自增改为自减,同步调整借位判断规则
  • 新增倒计时终止判断:当时间归0时自动停止计时,避免出现负数
  • 重置逻辑同步调整为恢复到00:13:00初始状态,原有Start/Stop按钮交互逻辑完全保留

你只需要替换原有JS代码即可,HTML、CSS无需做任何修改:

//Define vars to hold time values
let seconds = 0;
let minutes = 13;
let hours = 0;

//Define vars to hold "display" value
let displaySeconds = 0;
let displayMinutes = 0;
let displayHours = 0;

//Define var to hold setInterval() function
let interval = null;

//Define var to hold stopwatch status
let status = "stopped";

//Countdown function (logic to determine when to borrow next value, etc.)
function stopWatch(){

    seconds--;

    //Logic to determine when to borrow next value
    if(seconds < 0){
        seconds = 59;
        minutes--;

        if(minutes < 0){
            minutes = 59;
            hours--;
        }
    }

    //Auto stop when countdown reaches 00:00:00
    if(hours === 0 && minutes === 0 && seconds === 0){
        window.clearInterval(interval);
        document.getElementById("startStop").innerHTML = "Start";
        status = "stopped";
    }

    //If seconds/minutes/hours are only one digit, add a leading 0 to the value
    if(seconds < 10){
        displaySeconds = "0" + seconds.toString();
    }
    else{
        displaySeconds = seconds;
    }

    if(minutes < 10){
        displayMinutes = "0" + minutes.toString();
    }
    else{
        displayMinutes = minutes;
    }

    if(hours < 10){
        displayHours = "0" + hours.toString();
    }
    else{
        displayHours = hours;
    }

    //Display updated time values to user
    document.getElementById("display").innerHTML = displayHours + ":" + displayMinutes + ":" + displaySeconds;

}

function startStop(){
    //Block start operation if countdown has already ended
    if(hours === 0 && minutes === 0 && seconds === 0 && status === "stopped") return;

    if(status === "stopped"){
        //Start the countdown
        interval = window.setInterval(stopWatch, 1000);
        document.getElementById("startStop").innerHTML = "Stop";
        status = "started";
    }
    else{
        window.clearInterval(interval);
        document.getElementById("startStop").innerHTML = "Start";
        status = "stopped";
    }
}

//Function to reset the countdown
function reset(){
    window.clearInterval(interval);
    seconds = 0;
    minutes = 13;
    hours = 0;
    document.getElementById("display").innerHTML = "00:13:00";
    document.getElementById("startStop").innerHTML = "Start";
    status = "stopped";
}

内容的提问来源于stack exchange,提问作者eisha enan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 12:09:02