-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.js
192 lines (120 loc) · 4.96 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var timerLimit = 60; // default time setted in the application.
var stopTime = false; // help our program to break normal pomodoro flow.
var resetCustomTime = null; // just for making reset functionality work
var touchTimes = 0;
const wallpaperCount = 10; // It will only use 10 set of images for this app.
var totalTime = timerLimit;
document.getElementsByClassName("live-sec")[0].innerHTML = timerLimit;
document.getElementsByClassName("sec-limit")[0].innerHTML = timerLimit;
var tickSound = new Audio("sounds/tick.mp3");
var timeElapsedSound = new Audio("sounds/elapsed.mp3");
changeBackground();
for(var i=0; i < 4; i++){
document.getElementsByClassName("cntrl-btn")[i].addEventListener("click", function(event) {
timerLimit = event.toElement.childNodes[0].data * 60;
setTimeLimit();
})
}
document.getElementById("live-timer").addEventListener("touchstart", function(){
// To allow touch functionalities ie. for mobiles and tablets.
console.log(touchTimes,document.getElementsByClassName("start")[0].disabled)
if((touchTimes % 2) == 0) {
if(document.getElementsByClassName("start")[0].disabled == true) {
touchTimes++
} else {
touchTimes++;
startTimer();
}
} else {
stopTimer();
}
//touchTimes++;
})
document.addEventListener("keydown", function(keyboard){
// To enable keyboard functionalities for pomodaro
// Enter to start, Space to stop/ pause and R to restart the pomodoro.
if (keyboard.key === "r") {
resetTimer();
} else if(keyboard.key === "Enter") {
if(document.getElementsByClassName("start")[0].disabled == true) {
// prevent calling startTimer if already running.
{}
} else {
startTimer();
}
} else if(keyboard.key === " ") {
keyboard.preventDefault();
console.log("spaced");
stopTimer();
} else {
{}
}
})
function changeBackground() {
// responsible for loading different background image each time web app reloads / loads.
var randomNum = Math.round(Math.random() * 5);
if(randomNum != 0){
var targetImg = randomNum + ".jpg";
} else {
var targetImg = "1.jpg";
}
document.body.style.backgroundImage = "url(images/"+targetImg+")";
}
function setTimeLimit(){
if (document.getElementsByClassName("setTimeLimit")[0].value.length == 0) {
// If use has not provided any custom pomodoro time value, we will take look for custom button (if pressed )
// If no custom input is provided, default time of 60 seconds will be considered.
{}
} else {
// To take value from custom input box.
timerLimit = Math.round(document.getElementsByClassName("setTimeLimit")[0].value * 60);
document.getElementsByClassName("setTimeLimit")[0].value = null; // to empty value from custom time.
}
totalTime = timerLimit;
console.log(timerLimit, totalTime);
if( timerLimit != null) {
resetTimer();
if (!isNaN(parseInt(timerLimit))){
timerLimit = parseInt(timerLimit)
document.getElementsByClassName("sec-limit")[0].innerHTML = timerLimit;
} else {
{}
}
}
}
function resetTimer(){
// This function is responsible for resetting the program to defaults.
stopTime = true;
touchTimes = 0;
document.getElementsByClassName("live-sec")[0].style.wordWrap = "break-word";
document.getElementsByClassName("sec-limit")[0].innerHTML = totalTime; // to reset the value of elapsed time on screen.
document.getElementsByClassName("live-sec")[0].innerHTML = totalTime; // to reset the value of total time on screen.
timerLimit = totalTime; // to reset the timer.
}
function stopTimer(){
// This function will act as auxiliary to stop the timer once pressed.
touchTimes++;
stopTime = true;
}
function startTimer(){
// function responsible for playing sounds, making timer run and to update realtime values.
document.getElementsByClassName("start")[0].disabled = true;
tickSound.play()
document.getElementsByClassName("live-sec")[0].innerHTML = timerLimit;
document.getElementsByClassName("timeleftinMinutes")[0].innerHTML = ((timerLimit / totalTime) * 100).toFixed(1) + "% Time remaining."
if( timerLimit === 0) {
// Times UP !
tickSound.pause();
timeElapsedSound.play();
document.getElementsByClassName("start")[0].disabled = false;
return 0;
} else if (stopTime == true){
document.getElementsByClassName("start")[0].disabled = false;
tickSound.pause()
stopTime = false;
return 0;
} else {
timerLimit = timerLimit - 1; // responsible for dropping by one unit after each second.
setTimeout(startTimer, 1000); // To reexecute startTimer() again after waiting 1 sec.
}
}