简体   繁体   English

游戏计时器Javascript

[英]Game Timer Javascript

I'm creating a countdown timer. 我正在创建一个倒数计时器。 If seconds is equal to zero I have set 2 secs to var seconds . 如果seconds等于零,则将2 secs设置为var seconds Please help. 请帮忙。 I need to stop the program from looping after getting the 2 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);

Here is your fixed code: 这是您的固定代码:

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.

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

since i couldn't understand the code that's up in the question i wrote down my own timer. 因为我不明白问题中出现的代码,所以我写下了自己的计时器。 So take a look if it works out for you. 因此,看看是否适合您。

http://jsfiddle.net/9sEGz/ 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM