-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (35 loc) · 934 Bytes
/
Copy pathscript.js
File metadata and controls
41 lines (35 loc) · 934 Bytes
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
const start_btn = document.querySelector(".start");
const stop_btn = document.querySelector(".stop");
const reset_btn = document.querySelector(".reset");
const display = document.querySelector(".display");
let seconds = 0;
let interval = null;
// event listener
start_btn.addEventListener("click", start);
stop_btn.addEventListener("click", stop);
reset_btn.addEventListener("click", reset);
function timer() {
seconds++;
let hrs = Math.floor(seconds / 3600);
let mm = Math.floor((seconds - hrs * 3600) / 60);
let sec = seconds % 60;
if (sec < 10) sec = "0" + sec;
if (mm < 10) mm = "0" + mm;
if (hrs < 10) hrs = "0" + hrs;
display.innerText = `${hrs}:${mm}:${sec}`;
}
function start() {
if (interval) return;
else {
interval = setInterval(timer, 1000);
}
}
function stop() {
clearInterval(interval);
interval = null;
}
function reset() {
stop();
seconds=0;
display.innerText = "00:00:00";
}