-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.js
101 lines (85 loc) · 2.68 KB
/
Stopwatch.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
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
let startTime = 0;
let pausedTime = 0;
let isRunning = false;
let interval;
let lapCount = 1;
document.querySelector('.js-start-button').addEventListener('click',() =>{
startStop();
});
document.querySelector('.js-reset-button').addEventListener('click',() =>{
reset();
});
document.querySelector('.js-lap-button').addEventListener('click',() =>{
addLap();
});
document.body.addEventListener('keydown',(event) => {
if(event.key === 's'){
startStop();
}
else if(event.key === 'r'){
reset();
}
else if(event.key === 'l'){
addLap();
}
});
// Call updateTime initially to prevent the random number issue.
updateTime();
document.querySelector('.time').innerHTML = "00:00:00:00";
function startStop(){
if(isRunning){
clearInterval(interval);
pausedTime = Date.now();
document.querySelector('.js-start-button').innerHTML = "Start";
}
else{
if(startTime === 0){
startTime = Date.now();
}
else{
const currentTime = Date.now();
const pauseDuration = currentTime - pausedTime;
startTime += pauseDuration;
}
interval = setInterval(updateTime, 10);
document.querySelector('.js-start-button').innerHTML = "Stop";
}
isRunning = !isRunning;
}
function reset(){
clearInterval(interval);
document.querySelector('.js-start-button').innerHTML = "Start";
document.querySelector('.time').innerHTML = "00:00:00:00";
isRunning = false;
startTime = 0;
pausedTime = 0;
lapCount = 1;
clearLaps();
}
function updateTime(){
const currentTime = Date.now();
const elapsedTime = currentTime - startTime;
document.querySelector('.time').innerHTML = formatTime(elapsedTime);
}
function addLap(){
if(isRunning){
const currentTime = Date.now() - startTime;
const lapTime = formatTime(currentTime);
const lapList = document.querySelector('.lapList');
const lapItem = document.createElement('li');
lapItem.textContent = `Lap ${lapCount}: ${lapTime}`;
// Insert the new lap item at the top of the list
lapList.insertBefore(lapItem, lapList.firstChild);
lapCount++;
}
}
function clearLaps(){
document.querySelector('.lapList').innerHTML = '';
}
function formatTime(Time){
const hours = Math.floor(Time/3600000);
const minutes = Math.floor((Time % 3600000) / 60000);
const seconds = Math.floor((Time % 60000) / 1000);
const milliseconds = (Time % 1000).toString().slice(0,2);
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}:${milliseconds}`;
}