定时器不会停止并执行检查

时间:2017-03-03 09:09:42

标签: javascript jquery timer

functinality:

定时器将从0开始计数到3分钟的时间限制。用户必须按下按钮才能停止计时器,否则计时器将继续计数直至达到时间限制。

当达到3分钟的时间限制时,将显示警告:“游戏结束”。

问题:

计时器能够从0开始计时,但是,当时间限制为3分钟时,它不会停止显示警报。它将继续计数。

需要问一下为什么它不会停止计数器并在计时器达到3分钟的时间限制后提示警报?

代码:

url(r'^show_person/(?P<persnr>.*)', "company.views.show_person", name='show_person'),

def show_person(request, persnr):
    per = Person.objects.filter(Q(name=persnr)|Q(persnr=persnr)).all()
    context={'pers':pers}
    return render(request, 'company/person.html', context)
var GameTimer = 0;

var SetGameTimer;

var minutes = "";
var seconds = "";

function StartGame() {

  $("#Game_Elements").fadeIn({
    queue: false,
    complete: function() {

      RugbyGameTimer();

    }
  });
}
//Start Game Timer Count
function RugbyGameTimer() {


  //Start Game Timer
  SetGameTimer = setInterval(function() {

    GameTimer++;

    //get inidvidual value elements of the timer
    minutes = ('0' + Math.floor(GameTimer / 60)).slice(-2);
    seconds = ('0' + (GameTimer - minutes * 60)).slice(-2);

    //append timer to timer game elements
    $("#Game_Minute_timer").html(minutes);
    $("#Game_Second_timer").html(seconds);
  }, 1000);

  //Check Time value
  if (minutes < 3) {

    //Method call to check if button is pressed
    Interrupt_Game();

  } else if (minutes > 3) {
    //Check on time, if more than 10mins, automatically navigate to last game page

    //Stop Counter
    clearInterval(SetGameTimer);

    alert("Game Over");
  }
}


function InterruptGame() {

  //Method to do something
}

2 个答案:

答案 0 :(得分:2)

检查3分钟必须通过计时器方法检查每个滴答。

//Start Game Timer
    SetGameTimer = setInterval(function() {

        GameTimer++;

        //get inidvidual value elements of the timer
        minutes = ('0' + Math.floor(GameTimer / 60)).slice(-2);
        seconds = ('0' + (GameTimer - minutes * 60)).slice(-2);

        //append timer to timer game elements
        $("#Game_Minute_timer").html(minutes);
        $("#Game_Second_timer").html(seconds);

        //Check Time value
        if (minutes < 3) {

            //Method call to check if button is pressed
            Interrupt_Game();

        } else if (minutes >= 3) {
            //Check on time, if more than 10mins, automatically navigate to last game page

            //Stop Counter
            clearInterval(SetGameTimer);

            alert("Game Over");
        }
    }, 1000);
}

否则只会在游戏开始时检查一次。

答案 1 :(得分:0)

您正在将字符串与整数进行比较,这可能是其失败的原因。尝试测试秒数(计数器):

...
} else if (GameTimer > (3*60)) {
...