-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (62 loc) · 1.72 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
let hour = 0;
let minute = 0;
let second = 0;
let time;
let isStopped = false; // Add this line
function startTimer() {
isStopped = false;
document.getElementById("start").setAttribute("disabled","true");
time = setInterval(() => { timer(); }, 1000);
}
function stopTimer() {
if(isStopped) { // Add this condition
return;
}
isStopped = true; // Set isStopped to true when the timer is stopped
document.getElementById("start").removeAttribute("disabled");
clearInterval(time);
lap(hour,minute,second);
}
function resetTimer() {
stopTimer();
hour = 0;
minute = 0;
second = 0;
document.getElementById('hour').innerText = '00';
document.getElementById('min').innerText = '00';
document.getElementById('sec').innerText = '00';
}
function timer() {
if(isStopped) { // Add this condition
return;
}
second++;
if (second == 60) {
second = 0;
minute++;
}
if (minute == 60) {
minute = 0;
hour++;
}
if (hour == 24){
resetTimer();
}
document.getElementById('hour').innerText = returnData(hour);
document.getElementById('min').innerText = returnData(minute);
document.getElementById('sec').innerText = returnData(second);
}
function returnData(input) {
return input > 9 ? input : `0${input}`
}
function lap(h,m,s){
// If all are zero, don't add a lap
if(h === 0 && m === 0 && s === 0) {
return;
}
const tableLap = document.querySelector("#laps");
const timerDate = document.createElement("h2");
const timerNode = document.createTextNode(returnData(h)+":"+ returnData(m)+":"+returnData(s));
timerDate.appendChild(timerNode);
tableLap.appendChild(timerDate);
}