您好我正在创建一个运行定时器的Web程序,该定时器根据我创建的名为level的变量而变化。问题是当计时器达到0时,它继续倒计时。我知道clearInterval会解决这个问题但是当单击按钮增加到2级时,尽管为计时器增加了时间,但timeRemaining功能无法倒计时。
如果可以,请查看我的代码,并指出我的方向,我真的很感激。
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
};
答案 0 :(得分:0)
为什么要问两次if ( level !==0 )
?我会删除第二个level!==0
查询,然后尝试将timer == 0
更改为timer === 0
。