-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (54 loc) · 1.96 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
let timer;
let startTime;
let elapsedTime = 0;
let paused = true;
const display = document.getElementById('display');
const startStopButton = document.getElementById('startStop');
const resetButton = document.getElementById('reset');
const lapButton = document.getElementById('lap');
const lapsContainer = document.getElementById('laps');
startStopButton.addEventListener('click', startStop);
resetButton.addEventListener('click', reset);
lapButton.addEventListener('click', lap);
function startStop() {
if (paused) {
paused = false;
startTime = Date.now() - elapsedTime;
timer = setInterval(updateTime, 10); // Update every 10 milliseconds
startStopButton.textContent = 'Pause';
} else {
paused = true;
elapsedTime = Date.now() - startTime;
clearInterval(timer);
startStopButton.textContent = 'Start';
}
}
function reset() {
clearInterval(timer);
elapsedTime = 0;
paused = true;
display.textContent = '00:00:00';
startStopButton.textContent = 'Start';
lapsContainer.innerHTML = '';
}
function lap() {
if (!paused) {
const lapTime = formatTime(elapsedTime);
const lapElement = document.createElement('div');
lapElement.textContent = `Lap: ${lapTime}`;
lapsContainer.appendChild(lapElement);
}
}
function updateTime() {
elapsedTime = Date.now() - startTime;
display.textContent = formatTime(elapsedTime);
}
function formatTime(time) {
const milliseconds = Math.floor((time % 1000) / 10);
const seconds = Math.floor((time / 1000) % 60);
const minutes = Math.floor((time / (1000 * 60)) % 60);
const formattedMilliseconds = milliseconds.toString().padStart(2, '0');
const formattedSeconds = seconds.toString().padStart(2, '0');
const formattedMinutes = minutes.toString().padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}:${formattedMilliseconds}`;
}