如何使用 JavaScript 显示“倒计时”?

how to display " backward timer " using JavaScript?

我的 DOM 是这样的:

     <input type="number" id="input" value="" placeholder="Enter time in minutes">
    <button id="button">Go</button>
    <button id="reset">reset</button>

    <div class="timer">
        <div class="mint" id="mint"></div>
        <div class="sec" id="sec"></div>
    </div>

我的 JavaScript 是这样的:

let currentTime = 0;
let intervalClear;
let input = document.getElementById('input');
let button = document.getElementById('button')
button.addEventListener('click', ()=>{
    let value = input.value * 60000;
    function getTime(){
        currentTime++
        function backcount(currentTime){
            let output = value - currentTime
            console.log(output);
            const mint = document.getElementById('mint'),
            sec = document.getElementById('sec');

            let minute = Math.floor(output/60000)
            let second = ((output % 60000) / 1000).toFixed(0)
            mint.innerText = minute;
            sec.innerText = second;

            if(output == 0){
                clearInterval(intervalClear)
            }
        }
        backcount(currentTime);
    }
    getTime()
    intervalClear = setInterval(getTime, 1000)
})

const reset = document.getElementById('reset')
reset.addEventListener('click', ()=>{
    clearInterval(intervalClear);
    input.value = '';
})

现在我想在我的网页中显示值,但它没有更新。好像它冻结了。但是我的“setInterval()”运行 正确。

我该如何解决这个问题?需要帮助!

你需要的不是这个代码

let output = value - currentTime

使用这个

let output = value - (currentTime * 1000)

let currentTime = 0;
let intervalClear;
let input = document.getElementById('input');
let button = document.getElementById('button')
button.addEventListener('click', ()=>{
    let value = input.value * 60000;
    function getTime(){
        currentTime++
        function backcount(currentTime){
            let output = value - (currentTime * 1000)
            console.log(output);
            const mint = document.getElementById('mint'),
            sec = document.getElementById('sec');

            let minute = Math.floor(output/60000)
            let second = ((output % 60000) / 1000).toFixed(0)
            mint.innerText = minute;
            sec.innerText = second;

            if(output == 0){
                clearInterval(intervalClear)
            }
        }
        backcount(currentTime);
    }
    getTime()
    intervalClear = setInterval(getTime, 1000)
})

const reset = document.getElementById('reset')
reset.addEventListener('click', ()=>{
    clearInterval(intervalClear);
    input.value = '';
})
<input type="number" id="input" value="" placeholder="Enter time in minutes">
<button id="button">Go</button>
<button id="reset">reset</button>

<div class="timer">
    <div class="mint" id="mint"></div>
    <div class="sec" id="sec"></div>
</div>

根据@Oleg Barabanov 的回答,我发现了一个错误。如果您没有在文本框中输入任何值或第一次添加值,则单击“重置”并单击“开始”按钮,然后计数器从负值开始。我用这段代码解决了这个问题。

脚本

    var intervalClear;
    var input = document.querySelector('#input');
    var mint = document.querySelector('#mint');
    var sec = document.querySelector('#sec');
    var go_button = document.querySelector('#button');
    var reset_button = document.querySelector('#reset');

    go_button?.addEventListener('click', () => {
        if (input.value != '' && input.value != 0 && parseInt(input.value) != NaN) {
            startTimer(input.value, mint, sec);
        }
    });

    reset_button?.addEventListener('click', () => {
        clearInterval(intervalClear);
        mint.textContent = '00';
        sec.textContent = '00';
    });

    function startTimer(duration, minElement, secElement) {
        clearInterval(intervalClear);
        var timer = duration * 60, minutes, seconds;
        intervalClear = setInterval(function () {
            minutes = parseInt(timer / 60, 10);
            seconds = parseInt(timer % 60, 10);

            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;

            minElement.textContent = minutes;
            secElement.textContent = seconds;

            if (--timer < 0) {
                timer = duration;
            }

            if (minutes == 0 && seconds == 0) {
                clearInterval(intervalClear);
                mint.textContent = '00';
                sec.textContent = '00';
            }
        }, 1000);
    }

DOM

    <input type="number" id="input" placeholder="Enter time in minutes" >
    <button id="button">Go</button>
    <button id="reset">reset</button>

    <div class="timer">
      <div class="mint" id="mint">00</div>
      <div class="sec" id="sec">00</div>
    </div>