-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (39 loc) · 1.18 KB
/
Copy pathscript.js
File metadata and controls
45 lines (39 loc) · 1.18 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
let startTime;
let updatedTime;
let difference;
let timerId;
let running = false;
let display = document.getElementById('display');
let startStopBtn = document.getElementById('startStopBtn');
function startStop() {
if (!running) {
startTime = new Date().getTime() - (difference || 0);
timerId = setInterval(updateDisplay, 1000);
startStopBtn.textContent = 'Stop';
running = true;
} else {
clearInterval(timerId);
difference = new Date().getTime() - startTime;
startStopBtn.textContent = 'Start';
running = false;
}
}
function reset() {
clearInterval(timerId);
startTime = 0;
difference = 0;
running = false;
display.textContent = '00:00:00';
startStopBtn.textContent = 'Start';
}
function updateDisplay() {
updatedTime = new Date().getTime();
difference = updatedTime - startTime;
let hours = Math.floor(difference / 3600000);
let minutes = Math.floor((difference % 3600000) / 60000);
let seconds = Math.floor((difference % 60000) / 1000);
display.textContent = `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
}
function pad(unit) {
return (unit < 10) ? '0' + unit : unit;
}