-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer-brain.js
81 lines (70 loc) · 1.68 KB
/
timer-brain.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
const addTen = document.querySelector("#add-ten");
const addTwenty = document.querySelector("#add-twenty");
const addThirty = document.querySelector("#add-thirty");
const addFive = document.querySelector("#add-five");
const timer = document.querySelector("#timer");
const beep = document.querySelector("#beep");
const buzz = document.querySelector("#buzz");
let time = 0;
let intervalId;
beep.volume = 0.3;
buzz.volume = 0.3;
function startTimer() {
intervalId = setInterval(() => {
if (time > 0) {
time--;
} else {
clearInterval(intervalId);
swapButtons();
}
playSound();
setText(time);
}, 1000);
}
function setText(currentTime) {
if (time > 0) {
timer.innerText = currentTime;
} else {
timer.innerText = "--";
}
}
function playSound() {
if(time <= 3 && time > 0) {
beep.play();
} else if (time === 0) {
buzz.play();
}
}
function plusFive() {
clearInterval(intervalId);
time += 5;
setText(time);
startTimer();
}
function swapButtons() {
let buttons = document.querySelectorAll(".button");
for (let btn of buttons) {
btn.classList.toggle("invisible");
}
document.querySelector(".button-container").classList.toggle("centered");
}
addTen.addEventListener("click", () => {
setTimer(10);
swapButtons();
});
addTwenty.addEventListener("click", () => {
setTimer(20);
swapButtons();
});
addThirty.addEventListener("click", () => {
setTimer(30);
swapButtons();
});
addFive.addEventListener("click", () => {
plusFive();
});
function setTimer(number) {
time = number;
setText(time);
startTimer();
}