游戏计时器Javascript

时间:2014-04-12 03:46:37

标签: javascript jquery

我正在创建一个倒数计时器。如果秒数等于零,我已将2 secs设置为var seconds。请帮忙。我需要在获得2秒后停止程序循环

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);

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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);
}