2014-04-12 145 views
0

我正在創建倒數計時器。如果秒數等於零,我已將2 secs設置爲var seconds。請幫忙。我需要從獲得2秒遊戲計時器Javascript

var isWaiting = false; 
var isRunning = false; 
var seconds = 10; 
function GameTimer(){ 
    var minutes = Math.round((seconds - 30)/60); 
    var remainingSeconds = seconds % 60; 
    if(remainingSeconds < 10){ 
      remainingSeconds = "0" + remainingSeconds; 
    } 
    document.getElementById('waiting_time').innerHTML = minutes + ":" + remainingSeconds; 
    if(seconds == 0){ 
      isRunning = true; 
      seconds += 2; //I need to stop the program from looping after getting the 2 seconds 

    }else{ 
      isWaiting = true; 
      seconds--; 
    } 
} 
var countdownTimer = setInterval(GameTimer(),1000); 
+1

我想你至少應該告訴人們什麼是你的代碼錯誤?你有什麼問題嗎? –

+0

Hi @charinten。感謝您的答覆。我更新了這個問題。 – rrr

回答

0

這裏後循環停止程序的固定代碼:

var isWaiting = false; 
var isRunning = false; 
var seconds = 10; 
var countdownTimer; 
var finalCountdown = false; 

function GameTimer() { 
    var minutes = Math.round((seconds - 30)/60); 
    var remainingSeconds = seconds % 60; 
    if (remainingSeconds < 10) { 
     remainingSeconds = "0" + remainingSeconds; 
    } 
    document.getElementById('waiting_time').innerHTML = minutes + ":" + remainingSeconds; 
    if (seconds == 0) { 
     isRunning = true; 
     seconds += 2; 

     if (finalCountdown) { 
      clearInterval(countdownTimer); // Clear the interval to stop the loop 
     } else { 
      finalCountdown = true; // This will allow the 2 additional seconds only once. 
     } 

    } else { 
     isWaiting = true; 
     seconds--; 
    } 
} 
countdownTimer = setInterval(GameTimer, 1000); // Pass function reference, don't invoke it. 

工作演示:http://jsfiddle.net/nEjL4/1/

+0

感謝您的回覆。如果'seconds == 0',我需要再設置2秒。但是如果我使用'秒+ = 2',它會開始循環倒計時。如果我添加'clearInterval(countdownTimer);',它不會在2秒內創建一個倒計時。 – rrr

+0

所以你希望它從10到0進行計數,然後從2計數到0,然後停止? – AlienWebguy

+0

是的,先生。這正是我需要的 – rrr

0

,因爲我無法理解的代碼是在我寫下我自己的計時器的問題中出現的。所以看看它是否適合你。

http://jsfiddle.net/9sEGz/

var m=getId('m'), s=getId('s'), btn=getId('btn'), status=getId('status'), inc =getId('inc') , dec =getId('dec'), interval=null, time=0, min=0; 

btn.onclick = startCounter; 
inc.onclick = incTime; 
dec.onclick = decTime; 

function startCounter() { 

    if (time<=0) { 
     status.textContent='Increase the timer first!'; 
     time=0; 
     return; 
    } 
    status.textContent='Counting!'; 
    btn.textContent = 'Stop'; 
    btn.onclick = stopCounter; 
    interval = setInterval(function(){ 
     time--; 
     if (time<=0) { 
      stopCounter(); 
      status.textContent='Time\'s Up'; 
     } 
    setTime();   
    },200); 
} 

function stopCounter() { 
    btn.textContent = 'Start'; 
    btn.onclick = startCounter; 
    status.textContent='Stopped!'; 
    if (interval) clearInterval(interval); 
} 

function incTime(){ 
    time++; 
    setTime(); 
} 

function decTime(){ 
    time--; 
    setTime(); 
} 

function setTime() { 
     min= time/60; 
     if (time<10) s.textContent= '0'+Math.floor(time%60); 
     else s.textContent= Math.floor(time%60); 
     if (min<0) m.textContent= '00'; 
     else if (min<10) m.textContent= '0'+Math.floor(min); 
     else m.textContent= Math.floor(min); 
} 

function getId(x) { 
    return document.getElementById(x); 
} 
+0

我需要兩個倒計時器。第一次倒計時是等待比賽開始,第二次倒計時是跑步比賽。如果等待倒計時結束,我必須做遊戲倒計時。這就是我需要的。我上面的代碼,將第一個倒計時設置爲10秒。 '如果(秒== 0)'我設置另一個值秒,但它開始循環。我需要在倒計時2秒後停止循環。謝謝 – rrr