2016-01-28 52 views
0

您好我正在創建一個運行定時器的web程序,該定時器根據我創建的稱爲級別的變量而變化。問題是當計時器到達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

爲什麼你問了兩次if (level !==0)?我會刪除第二個level!==0查詢,然後我會嘗試將timer == 0更改爲timer === 0

+0

我做了這些改變,但沒有改變任何東西。定時器=== 0產生相同的結果,並且計數器繼續計數過去0. – user5854648

+0

好吧,現在我明白了。這裏有一個提示。準確地看時間剩餘功能。你注意到了什麼? – aGer

相關問題