-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (50 loc) · 1.66 KB
/
Copy pathscript.js
File metadata and controls
56 lines (50 loc) · 1.66 KB
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
let interval;
let startTime;
let accumulatedTime = 0;
let isRunning = false;
let lapCounter = 1;
function displayTime(ms) {
const minutes = Math.floor((ms / 60000) % 60).toString().padStart(2, '0');
const seconds = Math.floor((ms / 1000) % 60).toString().padStart(2, '0');
const milliseconds = Math.floor((ms % 1000) / 10).toString().padStart(2, '0');
return `${minutes}:${seconds}:${milliseconds}`;
};
function updateDisplay() {
const currentTime = Date.now() - startTime + accumulatedTime;
document.getElementById('display').textContent = displayTime(currentTime);
};
function startStop() {
if (isRunning) {
clearInterval(interval);
accumulatedTime += Date.now() - startTime;
isRunning = false;
document.querySelector('.btn').textContent = 'Start';
} else {
if (accumulatedTime === 0) {
startTime = Date.now();
} else {
startTime = Date.now(); // Update startTime to resume from where it was stopped
}
interval = setInterval(updateDisplay, 10);
isRunning = true;
document.querySelector('.btn').textContent = 'Pause';
}
};
function reset() {
clearInterval(interval);
isRunning = false;
accumulatedTime = 0;
document.querySelector('.btn').textContent = 'Start';
document.getElementById('display').textContent = '00:00:00';
document.getElementById('lapTimes').textContent = '';
lapCounter = 1;
}
function lap() {
if (isRunning) {
const currentTime = Date.now() - startTime + accumulatedTime;
const lapTime = displayTime(currentTime);
const lapTimesElement = document.getElementById('lapTimes');
lapTimesElement.innerHTML += `<p>Lap ${lapCounter}: ${lapTime}</p>`;
lapCounter++;
}
}