Skip to content

Commit 1466be1

Browse files
committed
resume method
1 parent 2e9078b commit 1466be1

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

app/js/timer.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,27 @@ angular.module('timer', [])
1616
$scope.start();
1717
});
1818

19+
$scope.$on('timer-resume', function (){
20+
$scope.resume();
21+
});
22+
1923
$scope.$on('timer-stop', function (){
2024
$scope.stop();
2125
});
2226

2327
$scope.start = $element[0].start = function () {
2428
$scope.startTime = new Date();
25-
updateTime();
26-
updateLater();
29+
tick();
30+
};
31+
32+
$scope.resume = $element[0].resume = function () {
33+
tick();
2734
};
2835

2936
$scope.stop = $element[0].stop = function () {
3037
$timeout.cancel($scope.timeoutId);
3138
};
3239

33-
function updateTime() {
34-
$scope.timeTaken = new Date() - $scope.startTime;
35-
$scope.minutes = Math.floor($scope.timeTaken / (1000 * 60));
36-
$scope.seconds = Math.floor(($scope.timeTaken - ($scope.minutes * 60)) / 1000) % 60;
37-
}
38-
39-
function updateLater() {
40-
$scope.timeoutId = $timeout(function () {
41-
updateTime();
42-
updateLater();
43-
}, $scope.interval);
44-
}
4540

4641
$element[0].stop = function () {
4742
$timeout.cancel($scope.timeoutId);
@@ -51,6 +46,16 @@ angular.module('timer', [])
5146
$timeout.cancel($scope.timeoutId);
5247
});
5348

49+
var tick = function (){
50+
$scope.timeTaken = new Date() - $scope.startTime;
51+
$scope.minutes = Math.floor($scope.timeTaken / (1000 * 60));
52+
$scope.seconds = Math.floor(($scope.timeTaken - ($scope.minutes * 60)) / 1000) % 60;
53+
54+
$scope.timeoutId = $timeout(function () {
55+
tick();
56+
}, $scope.interval);
57+
};
58+
5459
$scope.start();
5560
}
5661
};

0 commit comments

Comments
 (0)