【发布时间】:2016-08-25 10:13:14
【问题描述】:
我的计时器有点问题。我可能使它变得比应有的更复杂,因为我需要的是以下内容:
我需要从 00:00 数到 45:00,并且我需要能够在这些边界内停止和恢复计时器。
现在我有这个计时器代码:
<timer id="timer" autostart="false" start-time="coutingStart" end-time="countingEnd">{{mminutes}}:{{sseconds}}</timer>
countingStart 和 countingEnd 初始化如下:
var time = (45 * 60000); // should give me 45 minutes of time.
$scope.countingStart = (new Date()).getTime();
$scope.countingEnd = (new Date()).getTime() + time;
上面的代码有效,至少我认为有效。 我有一个带有此功能的按钮:
$scope.startGame = function() {
$scope.gameIsLive = true;
document.getElementById('timer').start();
};
它启动我的计数器,没问题,它至少从 00:00 开始。 但是我也有具有这些功能的按钮,这就是我遇到问题的地方。
$scope.PauseGame = function() {
switch ($scope.periodNum) {
case 1:
document.getElementById('timer').stop();
$scope.PauseIsActive = true;
break;
case 2:
document.getElementById('timer').stop();
$scope.PauseIsActive = true;
break;
}
};
$scope.ResumeGame = function() {
switch ($scope.periodNum) {
case 1:
document.getElementById('timer').resume();
$scope.PauseIsActive = false;
break;
case 2:
document.getElementById('timer').resume();
$scope.PauseIsActive = false;
break;
}
};
pauseGame() 和 resumeGame() 都按预期工作。他们正在暂停和恢复计时器。但是,当我暂停计时器说 00:10 并为自己计数 10 秒然后恢复它时,计时器现在停留在 00:20 这让我刚刚失去了 10 秒的计时器。
我可以认为我的问题在于 $scope.counterStart 和 $scope.counterEnd 的实例化。但我不确定。如何从 00:00 数到 45:00 并且仍然能够在需要时停止和恢复时钟?
Angular 计时器使用 Date 对象和毫秒来计算时间,所以我想我必须使用这种方法来获取现在的 00:00 并向前计数 45 分钟。是否可以通过停止和恢复功能以其他方式完成?
谢谢。
【问题讨论】:
-
github.com/siddii/angular-timer 是我现在使用的指令。
标签: javascript angularjs datetime timer