Skip to content

Commit a4fbd4d

Browse files
committed
addCDSeconds method work even for stopped countdown timers
1 parent 42d932b commit a4fbd4d

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

‎app/js/timer.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ angular.module('timer', [])
5858
$scope.start = $element[0].start = function () {
5959
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
6060
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
61-
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
61+
if (!$scope.countdown) {
62+
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
63+
}
6264
resetTimeout();
6365
tick();
6466
$scope.isRunning = true;
@@ -106,6 +108,9 @@ angular.module('timer', [])
106108
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
107109
$scope.countdown += extraSeconds;
108110
$scope.$digest();
111+
if (!$scope.isRunning) {
112+
$scope.start();
113+
}
109114
};
110115

111116
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
@@ -154,9 +159,7 @@ angular.module('timer', [])
154159
}
155160
else if ($scope.countdown <= 0) {
156161
$scope.stop();
157-
return;
158162
}
159-
160163
};
161164

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

‎dist/angular-timer.js‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.0.12 - 2014-02-03 9:30 PM
2+
* angular-timer - v1.0.12 - 2014-02-10 9:05 AM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2014 Siddique Hameed
@@ -52,6 +52,10 @@ angular.module('timer', [])
5252
$scope.clear();
5353
});
5454

55+
$scope.$on('timer-set-countdown', function (e, countdown) {
56+
$scope.countdown = countdown;
57+
});
58+
5559
function resetTimeout() {
5660
if ($scope.timeoutId) {
5761
clearTimeout($scope.timeoutId);
@@ -61,7 +65,9 @@ angular.module('timer', [])
6165
$scope.start = $element[0].start = function () {
6266
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
6367
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
64-
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
68+
if (!$scope.countdown) {
69+
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
70+
}
6571
resetTimeout();
6672
tick();
6773
$scope.isRunning = true;
@@ -109,6 +115,9 @@ angular.module('timer', [])
109115
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
110116
$scope.countdown += extraSeconds;
111117
$scope.$digest();
118+
if (!$scope.isRunning) {
119+
$scope.start();
120+
}
112121
};
113122

114123
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
@@ -143,13 +152,6 @@ angular.module('timer', [])
143152
return;
144153
}
145154
calculateTimeUnits();
146-
if ($scope.countdown > 0) {
147-
$scope.countdown--;
148-
}
149-
else if ($scope.countdown <= 0) {
150-
$scope.stop();
151-
return;
152-
}
153155

154156
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
155157
$scope.timeoutId = setTimeout(function () {
@@ -158,6 +160,13 @@ angular.module('timer', [])
158160
}, $scope.interval - adjustment);
159161

160162
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
163+
164+
if ($scope.countdown > 0) {
165+
$scope.countdown--;
166+
}
167+
else if ($scope.countdown <= 0) {
168+
$scope.stop();
169+
}
161170
};
162171

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

‎dist/angular-timer.min.js‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/angularjs-add-countdown-seconds.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<body ng-app="MyApp">
1818
<div ng-controller="MyAppController">
1919
<h1>AngularJS - Add Countdown Seconds Example</h1>
20-
<h3><timer countdown="100" interval="1000">{{countdown}}</timer></h3>
20+
<h3><timer countdown="10" interval="1000">{{countdown}}</timer></h3>
2121
<button ng-click="add5Seconds()">Add 5 Seconds</button>
2222
</div>
2323
<br/>

0 commit comments

Comments
 (0)