Skip to content

Commit cac81ad

Browse files
committed
countdown feature
1 parent 5697a63 commit cac81ad

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

app/js/timer.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ angular.module('timer', [])
33
return {
44
restrict: 'E',
55
replace: false,
6-
scope: {interval: '=interval'},
6+
scope: {
7+
interval: '=interval',
8+
countdownattr: '=countdown'
9+
},
710
controller: function ($scope, $element) {
811
if ($element.html().trim().length === 0) {
912
$element.append($compile('<span>{{millis}}</span>')($scope));
1013
}
1114

1215
$scope.startTime = null;
1316
$scope.timeoutId = null;
17+
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
1418

1519
$scope.$on('timer-start', function (){
1620
$scope.start();
@@ -44,6 +48,12 @@ angular.module('timer', [])
4448
});
4549

4650
var tick = function (){
51+
if ($scope.countdown > 0) {
52+
$scope.countdown--;
53+
}
54+
else if ($scope.countdown <= 0) {
55+
$scope.stop();
56+
}
4757
$scope.millis = new Date() - $scope.startTime;
4858
$scope.seconds = Math.floor(($scope.millis / 1000) % 60) ;
4959
$scope.minutes = Math.floor((($scope.millis / (1000*60)) % 60));

index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<li><a href="index.html#basic-timer">Basic Example</a></li>
8282
<li><a href="index.html#clock-timer">Clock Timer</a></li>
8383
<li><a href="index.html#progressbar-timer">Progressbar Timer</a></li>
84+
<li><a href="index.html#countdown-timer">Countdown Timer</a></li>
8485
</ul>
8586
</li>
8687
<li ng-non-bindable><a href="index.html#markup">&lt;timer/&gt;</a></li>
@@ -150,6 +151,15 @@ <h3><timer><div class="progress progress-striped active">
150151
</div>
151152
</section>
152153

154+
<section id="countdown-timer">
155+
<h3>Countdown Timer</h3>
156+
<div class="bs-docs-example">
157+
<p>The countdown timer <code ng-non-bindable>&lt;timer interval="1000" countdown="100"/&gt;</code> will start its countdown from 100 until it reaches 0 by ticking every second</p>
158+
<h3><timer interval="1000" countdown="100">{{countdown}}</timer></h3>
159+
</div>
160+
</section>
161+
162+
153163
<section id="markup">
154164
<h3>Markup</h3>
155165
<p>Timer directive can be declared using following options. By default, it will display milliseconds inside <code>span</code> tag.
@@ -189,6 +199,17 @@ <h4>Attributes</h4>
189199
1 millisecond
190200
</td>
191201
</tr>
202+
<tr>
203+
<td>
204+
countdown
205+
</td>
206+
<td>
207+
false
208+
</td>
209+
<td>
210+
211+
</td>
212+
</tr>
192213
</table>
193214

194215

@@ -204,15 +225,15 @@ <h4>Methods</h4>
204225
</thead>
205226
<tr>
206227
<td>start</td>
207-
<td>Triggered to start the timer. Will reset the start time</td>
228+
<td>Starts the timer</td>
208229
</tr>
209230
<tr>
210231
<td>stop</td>
211-
<td>Triggered to stop the timer.</td>
232+
<td>Stops the timer</td>
212233
</tr>
213234
<tr>
214235
<td>resume</td>
215-
<td>Triggered to resume the timer. Will NOT reset the start time</td>
236+
<td>Resumes the timer. Will NOT reset the start time</td>
216237
</tr>
217238
</table>
218239
</section>

0 commit comments

Comments
 (0)