如何更改依赖于变量的计时器

How to change timer that is dependent on variables?

本文关键字:计时器 变量 依赖于 何更改      更新时间:2023-09-26
嗨,我

正在创建一个 Web 程序,该程序运行一个计时器,该计时器根据我创建的称为 level 的变量而变化。问题是当计时器达到 0 时,它继续倒计时。我知道clearInterval会解决这个问题,但是当单击按钮增加到2级时,尽管向计时器添加了时间,但timeRemain函数无法倒计时。

如果您可以查看我的代码并指出我的方向,我将不胜感激。

var clock = setInterval(timeRun,1000);
var level = 1;
    if (level == 1 ){
        timer = 4000;
    }else if ( level == 2 ){
        timer = 10000;
    };
    function timeRemaining(endtime){
        var time = timer;
        var seconds = Math.floor( (time/1000) % 60 );
        var minutes = Math.floor( (time / 1000 / 60 ) % 60 );
    };
    function timerRun (){
        if ( level !==0 ){
            var time = timer;
            var time = timeRemaining(timer);
            timer-=1000;
            if (timer == 0 && level!== 0){
                console.log("Complete")
                //This continues the timer counting down past 0 infintiely Which I do not want. I want it to stop the timer at 0. Where am I going wrong?
                //When a button is clicked it will increase by a level. I know how to do that part.
            };      
        }else{
            console.log("Stopped");
            level=0;
            //This stops the timer
        };

为什么你问了两次if ( level !==0 )?我会删除第二个level!==0查询,然后我会尝试将timer == 0更改为timer === 0