Skip to content

Commit b9ee8a9

Browse files
Added intervalEvent attribute and an example
Added intervalEvent which broadcasts "event:timer-interval" on the $rootScope every interval. This allows periodic events that can be consumed, for instance, to save a document occasionally.
1 parent 686508c commit b9ee8a9

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎app/js/timer.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
angular.module('timer', [])
2-
.directive('timer', function ($timeout, $compile) {
2+
.directive('timer', function ($rootScope, $timeout, $compile) {
33
return {
44
restrict: 'E',
55
replace: false,
66
scope: {
77
interval: '=interval',
8-
countdownattr: '=countdown'
8+
countdownattr: '=countdown',
9+
intervalevent: '=intervalevent'
910
},
10-
controller: function ($scope, $element) {
11+
controller: function ($rootScope, $scope, $element) {
1112
if ($element.html().trim().length === 0) {
1213
$element.append($compile('<span>{{millis}}</span>')($scope));
1314
}
@@ -16,6 +17,7 @@ angular.module('timer', [])
1617
$scope.timeoutId = null;
1718
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
1819
$scope.isRunning = false;
20+
$scope.intervalEvents = 0;
1921

2022
$scope.$on('timer-start', function (){
2123
$scope.start();
@@ -29,6 +31,11 @@ angular.module('timer', [])
2931
$scope.stop();
3032
});
3133

34+
//example only: should consume this event in your controller
35+
$scope.$on('event:timer-interval', function () {
36+
$scope.intervalEvents++
37+
});
38+
3239
function resetTimeout() {
3340
if ($scope.timeoutId) {
3441
$timeout.cancel($scope.timeoutId);
@@ -68,6 +75,11 @@ angular.module('timer', [])
6875
$scope.seconds = Math.floor(($scope.millis / 1000) % 60) ;
6976
$scope.minutes = Math.floor((($scope.millis / (1000*60)) % 60));
7077
$scope.hours = Math.floor((($scope.millis / (1000*60*60)) % 24));
78+
79+
if($scope.intervalevent != undefined && $scope.intervalevent) {
80+
$rootScope.$broadcast('event:timer-interval');
81+
}
82+
7183
$scope.timeoutId = $timeout(function () {
7284
tick();
7385
}, $scope.interval);

‎index.html‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ <h3>
196196
</h3>
197197
</div>
198198
</section>
199+
<section id="intervalevent-timer">
200+
<h3>
201+
Interval Event</h3>
202+
203+
<div class="bs-docs-example">
204+
<p>
205+
The interval event <code ng-non-bindable="">&lt;timer interval=&quot;2000&quot; intervalEvent=&quot;true&quot;/&gt;</code>
206+
will fire 'event:timer-interval' on the $rootScope every interval.</p>
207+
208+
<h3>
209+
<timer interval="2000" intervalEvent="true">Event count: {{intervalEvents}}</timer>
210+
</h3>
211+
</div>
212+
</section>
199213
<section id="markup">
200214
<h3>
201215
Markup</h3>

0 commit comments

Comments
 (0)