-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (69 loc) · 3.07 KB
/
script.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
/* ================================================================= */
/* ======================= SECTION 3 =============================*/
/* ================================================================= */
let workTimeStarted = new Audio('./audio/work time started.m4a');
let workTimeEnded = new Audio('./audio/work time ended .m4a');
let restTimeStarted = new Audio('./audio/rest time started.m4a');
let restTimeEnded = new Audio('./audio/rest time ended .m4a');
document.getElementById('pomodoroStartButton').addEventListener('click', (event) => {
event.preventDefault();
workTimeStarted.play();
let pomodoroInputWork = document.getElementById('pomodoroInputWorkMinutes').value;
let pomodoroInputRest = document.getElementById('pomodoroInputRestMinutes').value;
pomodoroInputWork = pomodoroInputWork ? parseInt(pomodoroInputWork) : 0;
pomodoroInputRest = pomodoroInputRest ? parseInt(pomodoroInputRest) : 0;
console.log(pomodoroInputRest);
let workTimeInSecs = pomodoroInputWork * 60;
let restTimeInSecs = pomodoroInputRest * 60;
let workTime = setInterval(() => {
if (workTimeInSecs >= 0) {
let hours = Math.floor(workTimeInSecs / 3600);
let mins = Math.floor((workTimeInSecs % 3600) / 60);
let secs = Math.floor((workTimeInSecs % 3600) % 60);
mins = mins < 10 ? '0' + mins : mins;
secs = secs < 10 ? '0' + secs : secs;
document.getElementById('worktype').innerHTML = 'WORK TIME';
document.getElementById('pomodoroTimer').innerHTML = `${hours}:${mins}:${secs}`;
workTimeInSecs--;
} else if (!workTimeInSecs) {
console.log('something wrong ');
} else {
restTimeStarted.play();
document.getElementById('pomodoroTimer').innerHTML = `TIME UP !!`;
setTimeout(() => {
clearInterval(workTime);
let restTime = setInterval(() => {
if (restTimeInSecs >= 0) {
let hours = Math.floor(restTimeInSecs / 3600);
let mins = Math.floor((restTimeInSecs % 3600) / 60);
let secs = Math.floor((restTimeInSecs % 3600) % 60);
mins = mins < 10 ? '0' + mins : mins;
secs = secs < 10 ? '0' + secs : secs;
document.getElementById('worktype').innerHTML = 'REST TIME';
document.getElementById('pomodoroTimer').innerHTML = `${hours}:${mins}:${secs}`;
restTimeInSecs--;
} else if (!restTimeInSecs) {
console.log('something wrong ');
} else {
restTimeEnded.play();
document.getElementById('pomodoroTimer').innerHTML = `TIME UP !!`;
clearPomodoroFields();
clearInterval(restTime);
}
}, 1000);
}, 1000);
}
}, 1000);
function clearPomodoroFields() {
workTimeInSecs = 0;
restTimeInSecs = 0;
document.getElementById('worktype').innerHTML = 'SESSION ENDED';
document.getElementById('pomodoroTimer').innerHTML = 'HH : MM : SS';
document.getElementById('pomodoroInputWorkMinutes').value = '';
document.getElementById('pomodoroInputRestMinutes').value = '';
}
document.getElementById('pomodoroResetButton').addEventListener('click', () => {
clearInterval(workTime);
clearPomodoroFields();
});
});