-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (106 loc) · 3.67 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const timer = document.getElementById('timer');
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const resetButton = document.getElementById('reset');
const lapButton = document.getElementById('lap');
const lapList = document.getElementById('lapList');
let startTime = 0;
let elapsedTime = 0;
let timerInterval;
let lapCounter = 1;
function saveData() {
localStorage.setItem('lapCounter', lapCounter);
localStorage.setItem('lapList', lapList.innerHTML);
localStorage.setItem('elapsedTime', elapsedTime);
}
function loadData() {
lapCounter = parseInt(localStorage.getItem('lapCounter')) || 1;
lapList.innerHTML = localStorage.getItem('lapList') || '';
elapsedTime = parseInt(localStorage.getItem('elapsedTime')) || 0;
timer.textContent = formatTimer(elapsedTime);
}
loadData();
function startTimer() {
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(() => {
elapsedTime = Date.now() - startTime;
timer.textContent = formatTimer(elapsedTime);
}, 10);
startButton.disabled = true;
stopButton.disabled = false;
lapButton.disabled = false;
}
function stopTimer() {
clearInterval(timerInterval);
startButton.disabled = false;
stopButton.disabled = true;
lapButton.disabled = false;
}
function resetTimer() {
clearInterval(timerInterval);
elapsedTime = 0;
timer.textContent = "00:00:00";
lapList.innerHTML = '';
startButton.disabled = false;
stopButton.disabled = false;
lapButton.disabled = true;
lapCounter = 1;
saveData();
}
function formatTimer(elapsedTime) {
const hours = Math.floor(elapsedTime / (1000 * 60 * 60));
const minutes = Math.floor((elapsedTime % (1000 * 60 * 60)) / (1000 * 60))
const seconds = Math.floor((elapsedTime % (1000 * 60)) / 1000);
const mseconds = Math.floor((elapsedTime % 1000) / 10);
return (
(hours ? (hours > 9 ? hours : "0" + hours) : "00")
+ ":" +
(minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00")
+ ":" +
(seconds ? (seconds > 9 ? seconds : "0" + seconds) : "00")
+ "." +
(mseconds > 9 ? mseconds : "0" + mseconds)
);
}
function lap() {
const lapTime = formatTimer(elapsedTime);
const lapItem = document.createElement('li');
lapItem.textContent = `Lap ${lapCounter}: ${lapTime}`;
lapList.appendChild(lapItem);
lapCounter++;
saveData();
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function showNotification(message) {
const notification = document.createElement('div');
notification.classList.add('notification');
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, 2000);
}
lapList.addEventListener('click', (event) => {
if (event.target.tagName === 'LI') {
const lapTime = event.target.textContent.split(': ')[1];
copyToClipboard(lapTime);
showNotification(`Copied Lap Time: ${lapTime}`);
}
});
timer.addEventListener('click', () => copyToClipboard(timer.textContent));
lapList.addEventListener('click', (event) => {
if (event.target.tagName === 'LI') {
copyToClipboard(event.target.textContent.split(': ')[1]);
}
});
startButton.addEventListener('click', startTimer);
stopButton.addEventListener('click', stopTimer);
resetButton.addEventListener('click', resetTimer);
lapButton.addEventListener('click', lap);