Skip to content

Commit 248f4f1

Browse files
committed
Countdown set function and emit 0 as last value
With the listener one can set the countdown value using $broadcast('countdown-set', value). This might be an edge case, but I need to set a new countdown. Issue siddii#23 describes how to set the value on initialization but using this one can change the value whenever necessary. Also changed the order of execution of some code to make 0 the last emitted millis value for a countdown. In the original code the last emit message was the value just before 0. For example with a countdown with an interval of 1000 the last emitted millis value was 1000.
1 parent 7516570 commit 248f4f1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

app/js/timer.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ angular.module('timer', [])
4545
$scope.clear();
4646
});
4747

48+
$scope.$on('timer-set-countdown', function (e, countdown) {
49+
$scope.countdown = countdown;
50+
});
51+
4852
function resetTimeout() {
4953
if ($scope.timeoutId) {
5054
clearTimeout($scope.timeoutId);
@@ -136,13 +140,6 @@ angular.module('timer', [])
136140
return;
137141
}
138142
calculateTimeUnits();
139-
if ($scope.countdown > 0) {
140-
$scope.countdown--;
141-
}
142-
else if ($scope.countdown <= 0) {
143-
$scope.stop();
144-
return;
145-
}
146143

147144
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
148145
$scope.timeoutId = setTimeout(function () {
@@ -151,6 +148,15 @@ angular.module('timer', [])
151148
}, $scope.interval - adjustment);
152149

153150
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
151+
152+
if ($scope.countdown > 0) {
153+
$scope.countdown--;
154+
}
155+
else if ($scope.countdown <= 0) {
156+
$scope.stop();
157+
return;
158+
}
159+
154160
};
155161

156162
if ($scope.autoStart === undefined || $scope.autoStart === true) {

0 commit comments

Comments
 (0)