2014-04-12 120 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