-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (89 loc) · 3.05 KB
/
index.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
let countdown = 0; // variable to set/clear intervals
let secondes = 1500; // secondes left on the clock
let tempsTravail = 25;
let tempsPause = 5;
let enBreak = true;
let enPause = true;
let resultat = 0;
const status = document.querySelector("#status");
const timerDisplay = document.querySelector(".timerDisplay");
const startBtn = document.querySelector("#start-btn");
const resetBtn = document.querySelector("#reset");
const workMin = document.querySelector("#work-min");
const breakMin = document.querySelector("#break-min");
const resultatAffichage = document.querySelector("#resultatAffichage");
const testSon = document.querySelector("#testSon");
const alarm = document.createElement('audio'); // Un son de cloche qui sonne à la fin du travail.
alarm.setAttribute("src", "https://www.soundjay.com/misc/sounds/bell-ringing-05.mp3");
/* EVENT LISTENERS FOR START AND RESET BUTTONS */
startBtn.addEventListener('click', () => {
clearInterval(countdown);
enPause = !enPause;
if (!enPause) {
countdown = setInterval(timer, 1000);
}
})
resetBtn.addEventListener('click', () => {
clearInterval(countdown);
secondes = tempsTravail * 60;
countdown = 0;
enPause = true;
enBreak = true;
})
testSon.addEventListener('click', () => {
alarm.play()
})
/* TIMER - HANDLES COUNTDOWN */
function timer() {
secondes--;
if (secondes < 0) {
clearInterval(countdown);
alarm.currentTime = 0;
alarm.play();
if(enBreak){
resultat++;
}
secondes = (enBreak ? tempsPause : tempsTravail) * 60;
enBreak = !enBreak;
}
}
/* UPDATE WORK AND BREAK TIMES */
let increment = 1;
let incrementFunctions =
{
"#work-plus": function () { tempsTravail = Math.min(tempsTravail + increment, 60) },
"#work-minus": function () { tempsTravail = Math.max(tempsTravail - increment, 1) },
"#break-plus": function () { tempsPause = Math.min(tempsPause + increment, 60) },
"#break-minus": function () { tempsPause = Math.max(tempsPause - increment, 1) }
};
for (var key in incrementFunctions) {
if (incrementFunctions.hasOwnProperty(key)) {
document.querySelector(key).onclick = incrementFunctions[key];
}
}
/* UPDATE HTML CONTENT */
function countdownDisplay() {
let minutes = Math.floor(secondes / 60);
let remaindersecondes = secondes % 60;
timerDisplay.textContent = `${minutes}:${remaindersecondes < 10 ? '0' : ''}${remaindersecondes}`;
}
function buttonDisplay() {
if (enPause && countdown === 0) {
startBtn.textContent = "Commencer!";
} else if (enPause && countdown !== 0) {
startBtn.textContent = "Continue";
} else {
startBtn.textContent = "Pause";
}
}
function updateHTML() {
countdownDisplay();
buttonDisplay();
enBreak ? status.textContent = "Il faut travailler" : status.textContent = "Prend une pause!";
enPause ? statusAutre.textContent = "En attente..." : statusAutre.textContent = "TRAVAILLE! ÉCRIT! GO!!!";
workMin.textContent = tempsTravail;
breakMin.textContent = tempsPause;
resultatAffichage.textContent = resultat;
}
window.setInterval(updateHTML, 100);
document.onclick = updateHTML;