Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 游戏计时器(开始、暂停、停止)_Javascript_Jquery_Timer - Fatal编程技术网

Javascript 游戏计时器(开始、暂停、停止)

Javascript 游戏计时器(开始、暂停、停止),javascript,jquery,timer,Javascript,Jquery,Timer,我有一个计时器,游戏开始时就开始计时 我需要弄清楚如何在游戏结束时停止计时器,然后返回值(经过的时间) 这是我的计时器: function gameTimer(status) { $(".notes").text(status); if (gameStart == true) { gameStart = false; // so game will not repeat when image is clicked again to start game var timer

我有一个计时器,游戏开始时就开始计时

我需要弄清楚如何在游戏结束时停止计时器,然后返回值(经过的时间)

这是我的计时器:

function gameTimer(status) {

 $(".notes").text(status);

 if (gameStart == true) {
    gameStart = false; // so game will not repeat when image is clicked again to start game
    var timer = setInterval(calltimer, 1000);

    function calltimer() {
        $(".timerInner").text(time);

        if (status == true) {
            time++;

        }
    }

 }
}
以下是我对功能的设想:

gameTimer(start); // start timer
gameTimer(pause); // pause timer in case user needs to step away
gameTimer(stop); // stop timer and return value 
有没有关于如何实现这样的想法


谢谢,

按启动计时器的方式停止计时器。触发因素是什么?计时器在什么情况下启动

也许你想要这样的东西:

var gameStart = false;    

function gameTimer (status) {

  switch (status) {
    case "start":
      if (gameStart === false) {
        var timer = setInterval(callTimer, 1000);
        gameStart = true;
      }
    break;

    case "pause":
      if (gameStart === true && timer !== null) {
        clearInterval(timer);
        gameStart = false;
      }
    break;

    case "continue":
      if (gameStart === false && timer !== undefined && timer !== null) {
        timer = setInterval(callTimer, 1000);
        gameStart = true;
      }
    break;

    case "stop":
      if (timer !== null) {
        timer = null;
        gameStart = false;
      }
    break;
  }

  $(".notes").text(status);
}
正如您从代码中看到的,您可以使用“clearInterval(NameOfTimer)”方法暂停间隔,如果您想重置它,您必须重置计时器变量。 希望有帮助!:D

使用:

Function.prototype.scope = function(context) {
    var f = this;
    return function() {
        return f.apply(context, arguments);
    };
};

Timer = function() {
    this.tick = 0;
    this.intervalId = null;
    this.period = 1000; // in ms
    this.isPaused = false;
};

jQuery.extend(Timer.prototype, {

    onTick: function() {
        if (!this.isPaused) {
            this.tick++;
        }
    },

    start: function() {
        this.intervalId = setInterval(function() {this.onTick()}.scope(this), this.period);
    },

    pause: function() {
        this.isPaused = !this.isPaused;
    },

    stop: function() {
        clearInterval(this.intervalId);

        var result = this.tick;
        this.tick = 0;
        this.isPaused = false;

        return result;
    }
});

t = new Timer();
t.start();
t.pause();
t.stop();

谢谢,时间增量到哪里去了?时间++?你可以把时间增量放在你的函数“callTimer”中,你必须在某处定义这个函数。。。我的例子是一个简单的例子,你也许应该建立一些更硬和坚实的,并注意范围!因为===比仅仅==(我的意思是只在js中)更严格不关心类型: