Skip to content

Commit a505f17

Browse files
committed
Timer with events example
1 parent 686508c commit a505f17

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

app/js/timer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,16 @@ angular.module('timer', [])
6464
else if ($scope.countdown <= 0) {
6565
$scope.stop();
6666
}
67+
6768
$scope.millis = new Date() - $scope.startTime;
6869
$scope.seconds = Math.floor(($scope.millis / 1000) % 60) ;
6970
$scope.minutes = Math.floor((($scope.millis / (1000*60)) % 60));
7071
$scope.hours = Math.floor((($scope.millis / (1000*60*60)) % 24));
7172
$scope.timeoutId = $timeout(function () {
7273
tick();
7374
}, $scope.interval);
75+
76+
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
7477
};
7578

7679
$scope.start();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>AngularJS Example - Single Timer Example</title>
5+
<script src="../angular/angular.min.js"></script>
6+
<script src="../app/js/timer.js"></script>
7+
<script>
8+
angular.module('MyApp', ['timer']);
9+
function PollingController($scope, timerConsolePrefix) {
10+
$scope.timerRunning = true;
11+
$scope.timerConsole = '';
12+
13+
$scope.timerType = '';
14+
15+
$scope.startTimer = function (){
16+
$scope.$broadcast('timer-start');
17+
$scope.timerRunning = true;
18+
};
19+
20+
$scope.stopTimer = function (){
21+
$scope.$broadcast('timer-stop');
22+
$scope.timerRunning = false;
23+
};
24+
25+
$scope.$on('timer-tick', function (event, args) {
26+
$scope.timerConsole += $scope.timerType + ' - event.name = '+ event.name + ', timeoutId = ' + args.timeoutId.$$timeoutId + ', millis = ' + args.millis +'\n';
27+
});
28+
}
29+
30+
PollingController.$inject = ['$scope'];
31+
</script>
32+
</head>
33+
<body ng-app="MyApp">
34+
<div>
35+
<h1>AngularJS - Timer With Events Example</h1>
36+
<div ng-init="timerType = 'Polling Server'" ng-controller="PollingController" style="border: 1px darkgray dashed; padding: 15px">
37+
<h2>Example Simulating Polling Server every 5000 milliseconds</h2>
38+
<h3><timer interval="5000"/></h3>
39+
<textarea style="height: 100px;" row=20 cols="200">{{timerConsole}}</textarea>
40+
<br/>
41+
<button ng-click="startTimer('poll-server')" ng-disabled="timerRunning">Start Timer</button>
42+
<button ng-click="stopTimer('poll-server')" ng-disabled="!timerRunning">Stop Timer</button>
43+
</div>
44+
45+
<div ng-init="timerType = 'Saving Documents'" ng-controller="PollingController" style="border: 1px darkgray dashed; padding: 15px">
46+
<h2>Example Simulating Saving Document every 3000 milliseconds</h2>
47+
<h3><timer interval="3000"/></h3>
48+
<textarea style="height: 100px;" row=20 cols="200">{{timerConsole}}</textarea>
49+
<br/>
50+
<button ng-click="startTimer('poll-server')" ng-disabled="timerRunning">Start Timer</button>
51+
<button ng-click="stopTimer('poll-server')" ng-disabled="!timerRunning">Stop Timer</button>
52+
</div>
53+
54+
</div>
55+
<br/>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)