Skip to content

Commit ef7d748

Browse files
committed
Added countdown finished callback accessible from angular callback. Triggers when countdown finishes on it's own (not when it is stopped by a user). Useful if your app should take somea action when the countdown is reached.
1 parent 2d4843e commit ef7d748

6 files changed

Lines changed: 50 additions & 12 deletions

File tree

app/js/timer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ angular.module('timer', [])
88
startTimeAttr: '=startTime',
99
endTimeAttr: '=endTime',
1010
countdownattr: '=countdown',
11-
autoStart: '&autoStart'
11+
autoStart: '&autoStart',
12+
finishCallback: '&finishCallback'
1213
},
1314
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
1415

@@ -153,6 +154,7 @@ angular.module('timer', [])
153154
$scope.stop();
154155
$scope.millis = 0;
155156
calculateTimeUnits();
157+
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
156158
return;
157159
}
158160
calculateTimeUnits();
@@ -170,6 +172,7 @@ angular.module('timer', [])
170172
}
171173
else if ($scope.countdown <= 0) {
172174
$scope.stop();
175+
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
173176
}
174177
};
175178

dist/angular-timer.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
* angular-timer - v1.0.12 - 2014-03-18 4:02 PM
3-
* https://github.com/siddii/angular-timer
4-
*
5-
* Copyright (c) 2014 Siddique Hameed
6-
* Licensed MIT <https://github.com/siddii/angular-timer/blob/master/LICENSE.txt>
7-
*/
81
angular.module('timer', [])
92
.directive('timer', ['$compile', function ($compile) {
103
return {
@@ -15,7 +8,8 @@ angular.module('timer', [])
158
startTimeAttr: '=startTime',
169
endTimeAttr: '=endTime',
1710
countdownattr: '=countdown',
18-
autoStart: '&autoStart'
11+
autoStart: '&autoStart',
12+
finishCallback: '&finishCallback'
1913
},
2014
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
2115

@@ -112,14 +106,12 @@ angular.module('timer', [])
112106
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
113107
$scope.daysS = $scope.days==1 ? '' : 's';
114108

115-
116109
//add leading zero if number is smaller than 10
117110
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
118111
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
119112
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
120113
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
121114

122-
123115
}
124116
//determine initial values of time units and add AddSeconds functionality
125117
if ($scope.countdownattr) {
@@ -162,6 +154,7 @@ angular.module('timer', [])
162154
$scope.stop();
163155
$scope.millis = 0;
164156
calculateTimeUnits();
157+
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
165158
return;
166159
}
167160
calculateTimeUnits();
@@ -179,6 +172,7 @@ angular.module('timer', [])
179172
}
180173
else if ($scope.countdown <= 0) {
181174
$scope.stop();
175+
if($scope.finishCallback) $scope.$eval($scope.finishCallback);
182176
}
183177
};
184178

dist/angular-timer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/docs.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ angular.module('timer-demo',['timer']).controller('TimerDemoController',['$scope
3434
}
3535
});
3636
};
37+
38+
$scope.callbackTimer={};
39+
$scope.callbackTimer.status='Running';
40+
$scope.callbackTimer.callbackCount=0;
41+
$scope.callbackTimer.finished=function(){
42+
$scope.callbackTimer.status='COMPLETE!!';
43+
$scope.callbackTimer.callbackCount++;
44+
$scope.$apply();
45+
};
3746
}]);

index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ <h3 class="plural-counter">
181181
</div>
182182
</section>
183183

184+
185+
<section id="finish-callback-timer">
186+
<h3>Countdown Finished Callback</h3>
187+
188+
<div class="bs-docs-example">
189+
<p>
190+
A countdown timer that updates a value once the callback is reached
191+
<code ng-non-bindable="">
192+
&lt;timer countdown="3" interval="1000" finish-callback="callbackTimer.finished()"&gt;{{seconds}} second{{secondsS}}&lt;/timer&gt;
193+
</code>
194+
195+
<h3 class="counter">
196+
<timer countdown="3" interval="1000" finish-callback="callbackTimer.finished()">{{seconds}} second{{secondsS}} </timer>
197+
</h3>
198+
Timer: <span class="timer-status">{{callbackTimer.status}}</span>
199+
Callbacks: <span class="timer-callbacks">{{callbackTimer.callbackCount}}</span>
200+
201+
</div>
202+
</section>
203+
184204
<section id="markup">
185205
<h3>
186206
Markup</h3>

test/e2e/scenarios.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,17 @@ describe('Angular Timer E2E Tests', function () {
109109
expect(element('#clock-timer-leading-zero timer').html()).toMatch(/00 minutes,/);
110110
expect(element('#clock-timer-leading-zero timer').html()).toMatch(/11 seconds./);
111111
});
112+
113+
it('Countdown finish - Should fire callback on completion', function () {
114+
115+
116+
expect(element('#finish-callback-timer .timer-status').html()).toBe('Running');
117+
expect(element('#finish-callback-timer .timer-callbacks').html()).toBe('0');
118+
119+
sleep(5);
120+
expect(element('#finish-callback-timer .timer-status').html()).toBe('COMPLETE!!');
121+
expect(element('#finish-callback-timer .timer-callbacks').html()).toBe('1');
122+
123+
});
112124

113125
});

0 commit comments

Comments
 (0)