setInterval - 倒计时没有按预期工作

setInterval - not working as expected with countdown timing

function countdown() {
        setInterval(function() {
            seconds = seconds - 1;

            if(seconds < 1) {
                endGame();
            }
            else {
                if(seconds < 60) {
                    //...
                }
                $('.Timer').text(seconds);      
            }

        }, 1000);
    }

到达0时调用结束游戏函数
结束游戏功能只是用重新开始按钮重新开始游戏。

如果我按下重启按钮,秒数现在会一一减少。

function reset() {
        seconds = 60;           
    }

第一次效果很好。秒数每秒减少一秒。但是在几次重启之后,秒数减少得非常快。它在五到十秒内达到零。

function startGame() {
        reset();            
        countdown();
        $('.start-button').hide();
    }   

function endGame() {
        $('.start-button').show();
    }

一旦倒计时结束,您需要清除计时器,否则会有多个计时器实例 运行 导致上述行为

function countdown() {
    var interval = setInterval(function () {
        seconds = seconds - 1;

        if (seconds < 1) {
            clearInterval(interval);
            endGame();
        } else {
            if (seconds < 60) {
                //...
            }
            $('.Timer').text(seconds);
        }

    }, 1000);
}