forked from rocketacademy/match-game-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
86 lines (77 loc) · 2.13 KB
/
timer.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
// global variables
let seconds = 0;
let minutes = 0;
let hours = 0;
let canStart = true;
let ref;
let canReset = true;
const delayMilliSeconds = 10;
// DOM elements
const elapsedTime = document.createElement('div');
const hour = document.createElement('span');
const minute = document.createElement('span');
const second = document.createElement('span');
const startButton = document.createElement('button');
const stopButton = document.createElement('button');
const resetButton = document.createElement('button');
hour.innerText = `${0}hr(s)`;
minute.innerText = `${0}min(s)`;
second.innerText = `${0}s`;
startButton.innerText = 'Start';
stopButton.innerText = 'Stop';
resetButton.innerText = 'Reset';
elapsedTime.classList.add('elapsedTime');
document.body.appendChild(elapsedTime);
document.body.appendChild(startButton);
document.body.appendChild(stopButton);
document.body.appendChild(resetButton);
elapsedTime.appendChild(hour);
elapsedTime.appendChild(minute);
elapsedTime.appendChild(second);
// function to start timer
const startTimer = () => {
second.innerText = `${seconds}s`;
minute.innerText = `${minutes}min(s)`;
hour.innerText = `${hours}hr(s)`;
seconds += 1;
second.innerText = `${seconds}s`;
if (seconds === 59) {
seconds = 0;
minutes += 1;
minute.innerText = `${minutes}min(s)`;
}
if (minutes === 59) {
seconds = 0;
minutes = 0;
hours += 1;
hour.innerText = `${hours}hr(s)`;
}
};
// function to reset timer
const resetTimer = () => {
if (canReset === true) {
clearInterval(ref);
second.innerText = `${0}s`;
minute.innerText = `${0}min(s)`;
hour.innerText = `${0}hr(s)`;
seconds = 0;
minutes = 0;
hours = 0;
canStart = true;
}
};
// start timer when start button is clicked
startButton.addEventListener('click', () => {
if (canStart === true) {
ref = setInterval(startTimer, delayMilliSeconds);
}
canStart = false;
});
// stop timer when stop button is clicked
stopButton.addEventListener('click', () => {
clearInterval(ref);
canStart = true;
canReset = true;
});
// reset timer when reset button is clicked
resetButton.addEventListener('click', resetTimer);