-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (158 loc) · 4.62 KB
/
index.html
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<html>
<head>
<title id="stopwatch_on_title">00:00:00</title>
<style>
body {
background: #000000;
color: white;
}
button {
width: 150px;
height: 45px;
border: 3px soldi white;
font-size: 25px;
border-radius: 50px;
background: #000000;
color: white;
cursor: pointer;
outline: 0;
}
button:hover {
background: aqua;
border: solid 1px white;
color: black;
}
#stopwatch {
font-size: 250px;
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%,-55%);
color: rgb(34, 255, 0);
font-weight: bold;
}
#buttons {
position: absolute;
top: 75%;
left: 48.4%;
transform: translate(-51.6%,-45%);
}
#buttons li {
display: inline;
padding-left: 50px;
}
#laps {
position: absolute;
top: 90%;
left: 50%;
transform: translate(-50%,-50%);
list-style: none;
padding: 0;
}
.lap {
display: flex;
justify-content: space-between;
align-items: center;
margin: 5px 0;
padding: 10px;
background: #000000;
border: 2px solid white;
border-radius: 10px;
box-shadow: 0px 0px 5px 1px white;
width: 400px;
}
.lap-note {
font-size: 20px;
color: aqua;
margin-left: 10px;
}
</style>
</head>
<body>
<div id="stopwatch">00:00:00</div>
<ul id="buttons">
<li><button id="start_pause" onclick="runTimer()">Start</button></li>
<li><button onclick="resetTimer()">Reset</button></li>
<li><button onclick="createLap()">Lap</button></li>
</ul>
<ul id="laps"></ul>
<script>
document.addEventListener("keydown", function(e) {
if ("Space" === e.code) {
runTimer();
}
});
const timer = document.getElementById("stopwatch");
const timer_on_title = document.getElementById("stopwatch_on_title");
const start_pause_button = document.getElementById("start_pause");
let hr = 0, min = 0, sec = 0, stoptime = true, laps = [];
function runTimer() {
if (stoptime) {
stoptime = false;
timerCycle();
start_pause_button.innerHTML = "Pause";
} else {
stoptime = true;
start_pause_button.innerHTML = "Resume";
}
}
function timerCycle() {
if (!stoptime) {
sec = parseInt(sec);
min = parseInt(min);
hr = parseInt(hr);
if (sec == 60) {
min += 1;
sec = 0;
}
if (min == 60) {
hr += 1
min = 0;
}
sec = (sec < 10) ? "0" + sec : sec;
min = (min < 10) ? "0" + min : min;
hr = (hr < 10) ? "0" + hr : hr;
const time = hr + ":" + min + ":" + sec;
timer.innerHTML = time;
timer_on_title.innerHTML = time;
sec = parseInt(sec) + 1;
setTimeout("timerCycle()", 1000);
}
}
function resetTimer() {
stoptime = true;
hr = 0;
min = 0;
sec = 0;
timer.innerHTML = "00:00:00";
start_pause_button.innerHTML = "Start";
laps = [];
updateLaps();
}
function createLap() {
if (!stoptime) {
const lapTime = timer.innerHTML;
const note = prompt("Add a note (optional)");
const lap = { time: lapTime, note: note };
laps.push(lap);
updateLaps();
}
}
function updateLaps() {
const lapsList = document.getElementById("laps");
lapsList.innerHTML = "";
for (let i = 0; i < laps.length; i++) {
const lap = laps[i];
const li = document.createElement("li");
li.setAttribute("class", "lap");
const lapIndex = i + 1;
li.innerHTML = "<div>Lap " + lapIndex + ": " + lap.time + "</div>";
if (lap.note) {
li.innerHTML += "<div class='lap-note'>" + lap.note + "</div>";
}
lapsList.appendChild(li);
}
}
</script>
</body>
</html>