-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
46 lines (37 loc) · 1.1 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
const $startBtn = document.querySelector('#start-btn');
const $stopBtn = document.querySelector('#stop-btn');
const $resetBtn = document.querySelector('#reset-btn');
const $seconds = document.querySelector('#seconds');
const $minutes = document.querySelector('#minutes');
let intervalId;
let seconds = 0;
let minutes = 0;
$startBtn.addEventListener('click', startStopwatch)
$stopBtn.addEventListener('click', stopStopwatch)
$resetBtn.addEventListener('click', resetStopwatch)
function formatDigits(number) {
return number < 10 ? '0' + number : number;
}
function startStopwatch() {
intervalId = setInterval(function() {
$seconds.innerText = formatDigits(seconds);
seconds++;
if(seconds > 59) {
minutes++;
seconds = 0;
}
$seconds.innerText = formatDigits(seconds);
$minutes.innerText = formatDigits(minutes);
}, 1000);
}
function stopStopwatch() {
if(intervalId) {
clearInterval(intervalId)
}
}
function resetStopwatch() {
seconds = 0;
minutes = 0;
$seconds.innerText = '00';
$minutes.innerText = '00';
}