-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerFuncs.js
115 lines (104 loc) · 3.29 KB
/
TimerFuncs.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var app = angular.module('PomodoroApp', []).controller('MainCtrl', function($scope, $interval) {
$scope.breakLength = 5;
$scope.sessionLength = 25;
$scope.timeLeft = $scope.sessionLength;
$scope.fillHeight = '0%';
$scope.sessionName = 'Session';
$scope.currentTotal;
var runTimer = false;
var secs = 60 * $scope.timeLeft;
$scope.originalTime = $scope.sessionLength;
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
return (
(h > 0 ? h + ":" + (m < 10 ? "0" : "") : "") + m + ":" + (s < 10 ? "0" : "") + s
);
}
// Change default session length
$scope.sessionLengthChange = function(time) {
if (!runTimer){
if ($scope.sessionName === 'Session') {
$scope.sessionLength += time;
if ($scope.sessionLength < 1) {
$scope.sessionLength = 1;
}
$scope.timeLeft = $scope.sessionLength;
$scope.originalTime = $scope.sessionLength;
secs = 60 * $scope.sessionLength;
}
}
}
// Change default break length
$scope.breakLengthChange = function(time) {
if (!runTimer){
$scope.breakLength += time;
if ($scope.breakLength < 1) {
$scope.breakLength = 1;
}
if ($scope.sessionName === 'Break!') {
$scope.timeLeft = $scope.breakLength;
$scope.originalTime = $scope.breakLength;
secs = 60 * $scope.breakLength;
}
}
}
$scope.reset = function() {
$interval.cancel(runTimer);
runTimer = false;
$scope.timeLeft = $scope.sessionLength;
secs = 60 * $scope.timeLeft;
}
$scope.reset();
$scope.toggleTimer = function() {
if (!runTimer) {
if ($scope.currentName === 'Sesson') {
$scope.currentLength = $scope.sessionLength;
} else {
$scope.currentLength = $scope.breakLength;
}
updateTimer();
runTimer = $interval(updateTimer, 1000);
} else {
$interval.cancel(runTimer);
runTimer = false;
}
}
function updateTimer() {
secs -= 1;
if (secs < 0) {
// countdown is finished
// Play audio
var wav = 'Nature-sounds-birds.mp3';
var audio = new Audio(wav);
audio.play();
// toggle break and session
$scope.fillColor = '#333333';
if ($scope.sessionName === 'Break!') {
$scope.sessionName = 'Session';
$scope.currentLength = $scope.sessionLength;
$scope.timeLeft = 60 * $scope.sessionLength;
$scope.originalTime = $scope.sessionLength;
secs = 60 * $scope.sessionLength;
} else {
$scope.sessionName = 'Break!';
$scope.currentLength = $scope.breakLength;
$scope.timeLeft = 60 * $scope.breakLength;
$scope.originalTime = $scope.breakLength;
secs = 60 * $scope.breakLength;
}
} else {
if ($scope.sessionName === 'Break!') {
$scope.fillColor = '#FF4444';
} else {
$scope.fillColor = '#99CC00';
}
$scope.timeLeft = secondsToHms(secs);
var denom = 60 * $scope.originalTime;
var perc = Math.abs((secs / denom) * 100 - 100);
$scope.fillHeight = perc + '%';
}
}
});