forked from siddii/angular-timer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-timer.js
91 lines (76 loc) · 3.39 KB
/
angular-timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
angular.module('timer', [])
.directive('timer', function ($timeout, $compile) {
return {
restrict: 'E',
replace: false,
scope: {
interval: '=interval',
startTimeAttr: '=startTime',
countdownattr: '=countdown'
},
controller: function ($scope, $element) {
if ($element.html().trim().length === 0) {
$element.append($compile('<span>{{millis}}</span>')($scope));
}
$scope.startTime = null;
$scope.timeoutId = null;
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
$scope.isRunning = false;
$scope.$on('timer-start', function () {
$scope.start();
});
$scope.$on('timer-resume', function () {
$scope.resume();
});
$scope.$on('timer-stop', function () {
$scope.stop();
});
function resetTimeout() {
if ($scope.timeoutId) {
clearTimeout($scope.timeoutId);
}
}
$scope.start = $element[0].start = function () {
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
resetTimeout();
tick();
};
$scope.resume = $element[0].resume = function () {
resetTimeout();
$scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime);
tick();
};
$scope.stop = $element[0].stop = function () {
$scope.stoppedTime = new Date();
resetTimeout();
$scope.timeoutId = null;
};
$element.bind('$destroy', function () {
resetTimeout();
});
var tick = function () {
if ($scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
}
$scope.millis = new Date() - $scope.startTime;
if ($scope.countdown > 0) {
$scope.millis = $scope.countdown * 1000;
}
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
$scope.minutes = Math.floor((($scope.millis / (1000 * 60)) % 60));
$scope.hours = Math.floor((($scope.millis / (1000 * 60 * 60)) % 24));
$scope.days = Math.floor((($scope.millis / (1000 * 60 * 60)) / 24));
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
$scope.timeoutId = setTimeout(function () {
tick();
$scope.$apply();
}, $scope.interval);
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
};
$scope.start();
}
};
});