-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (71 loc) · 1.95 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
80
81
82
83
let sec = 0;
let min = 0;
let timer = false;
let timeinterval;
const count = document.querySelector('.count');
const start = document.querySelector(".playbtn");
const stopbtn = document.querySelector(".stop");
const changingbtn = document.querySelector('.stopbtn');
const resetbtn = document.querySelector(".reset");
stopbtn.style.opacity = 0;
resetbtn.style.opacity = 0;
function timeupdate() {
sec++;
if (sec === 60) {
sec = 0;
min++;
}
const formattedSec = sec < 10 ? `0${sec}` : sec;
const formattedMin = min < 10 ? `0${min}` : min;
if (count) {
count.innerText = `${formattedMin}:${formattedSec}`;
}
}
function reset() {
sec = 0;
min = 0;
if (count) {
count.innerHTML = '00:00';
}
}
function toggleTimer() {
start.style.opacity = timer ? 0 : '';
count.style.opacity = timer ? '' : 1;
stopbtn.style.opacity = timer ? 1 : 0;
resetbtn.style.opacity = timer ? 1 : 0;
}
start.addEventListener('click', () => {
timer = !timer;
toggleTimer();
start.style.opacity = 0
if (timer) {
clearInterval(timeinterval);
timeinterval = setInterval(timeupdate, 1000);
} else {
clearInterval(timeinterval);
}
});
let clicks = 0;
stopbtn.addEventListener('click', () => {
timer = !timer;
clicks++;
changingbtn.src = clicks % 2 !== 0 ? 'svg/play-circle.svg' : 'svg/pause.svg';
document.querySelector(".s").innerHTML = clicks % 2 !== 0 ? 'RESUME' : 'STOP';
clearInterval(timeinterval);
if (timer) {
timeinterval = setInterval(timeupdate, 1000);
}
});
resetbtn.addEventListener('click', () => {
reset();
clearInterval(timeinterval);
changingbtn.src = 'svg/pause.svg';
document.querySelector(".s").innerHTML = 'STOP';
start.style.opacity = '';
stopbtn.style.opacity = 0;
resetbtn.style.opacity = 0;
count.style.opacity = '';
clicks = 0;
timer = false;
start.style.opacity = '';
});