我的計時器有點麻煩。我可能已經使它比它應該更復雜,因爲我需要的是以下內容:角度計時器 - .stop()和.resume()問題
我需要計數從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開始ATLEAST。 但是,然後我有這些功能的按鈕,這也是我遇到我的問題。
$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計數,並且在需要時仍能夠停止和恢復時鐘?
角度計時器使用Date對象和毫秒來計算時間,所以我想我必須使用這種方法來獲得00:00這是現在和計數45分鐘前進。停止和恢復功能可以以其他方式完成嗎?
感謝。
https://github.com/siddii/angular-timer的是,我現在使用的指令。 –