Skip to content

Commit

Permalink
Countdown set function and emit 0 as last value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnparn committed Feb 7, 2014
1 parent 7516570 commit 248f4f1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ angular.module('timer', [])
$scope.clear();
});

$scope.$on('timer-set-countdown', function (e, countdown) {
$scope.countdown = countdown;
});

function resetTimeout() {
if ($scope.timeoutId) {
clearTimeout($scope.timeoutId);
Expand Down Expand Up @@ -136,13 +140,6 @@ angular.module('timer', [])
return;
}
calculateTimeUnits();
if ($scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
return;
}

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

$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});

if ($scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
return;
}

};

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

0 comments on commit 248f4f1

Please sign in to comment.