-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (42 loc) · 1.43 KB
/
Copy pathscript.js
File metadata and controls
49 lines (42 loc) · 1.43 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
// 获取DOM元素
const timerDisplay = document.getElementById('timer');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const resetBtn = document.getElementById('resetBtn');
let startTime;
let elapsedTime = 0;
let timerInterval;
// 格式化时间显示
function formatTime(milliseconds) {
const hours = Math.floor(milliseconds / 3600000);
const minutes = Math.floor((milliseconds % 3600000) / 60000);
const seconds = Math.floor((milliseconds % 60000) / 1000);
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
// 更新计时器显示
function updateDisplay() {
const currentTime = Date.now();
elapsedTime = currentTime - startTime;
timerDisplay.textContent = formatTime(elapsedTime);
}
// 开始计时
startBtn.addEventListener('click', () => {
startBtn.disabled = true;
pauseBtn.disabled = false;
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(updateDisplay, 10);
});
// 暂停计时
pauseBtn.addEventListener('click', () => {
startBtn.disabled = false;
pauseBtn.disabled = true;
clearInterval(timerInterval);
});
// 重置计时器
resetBtn.addEventListener('click', () => {
startBtn.disabled = false;
pauseBtn.disabled = true;
clearInterval(timerInterval);
elapsedTime = 0;
timerDisplay.textContent = '00:00:00';
});