-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (32 loc) · 1.13 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
let timer;
let isRunning = false;
let milliseconds = 0;
let millisecondsSteps = 40;
function startStop() {
if (isRunning) {
clearInterval(timer);
} else {
timer = setInterval(updateDisplay, 40); // Update every 40 milliseconds
}
isRunning = !isRunning;
}
function reset() {
clearInterval(timer);
isRunning = false;
milliseconds = 0;
updateDisplay();
}
function updateDisplay() {
const display = document.getElementById('display');
const formattedTime = formatTime(milliseconds);
display.innerText = formattedTime;
milliseconds += 40; // Increment by 40 milliseconds, wrapping around 0-39
}
function formatTime(ms) {
const date = new Date(ms);
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
const formattedMilliseconds = Math.floor((ms % 1000) / (1000 / millisecondsSteps)).toString().padStart(2, '0'); // Display only two digits up to milliseconds steps
return `${hours}:${minutes}:${seconds}.${formattedMilliseconds}`;
}