-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (42 loc) · 1.45 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
const startButton = document.querySelector('[data-action="start"]');
const stopButton = document.querySelector('[data-action="stop"]');
const resetButton = document.querySelector('[data-action="reset"]');
const minutes = document.querySelector('.minutes');
const seconds = document.querySelector('.seconds');
let timerTime = 00;
let isRunning = false;
let interval;
function startTimer() {
if (isRunning) return;
isRunning = true;
interval = setInterval(incrementTimer, 1000);
}
function stopTimer() {
if (!isRunning) return;
isRunning = false;
clearInterval(interval);
}
function resetTimer() {
stopTimer();
timerTime = 0;
minutes.innerText = '00';
seconds.innerText = '00';
}
function incrementTimer() {
timerTime++;
const numOfMinutes = Math.floor(timerTime / 60);
const numOfSeconds = timerTime % 60;
minutes.innerText = pad(numOfMinutes);
seconds.innerText = pad(numOfSeconds);
}
function pad(number) {
return (number < 10) ? '0' + number : number;
// if (number < 10) {
// return '0' + number;
// } else {
// return number;
// }
}
startButton.addEventListener('click', startTimer);
stopButton.addEventListener('click', stopTimer);
resetButton.addEventListener('click', resetTimer);