startButton.onclick = function() { if (timer == null) { timer = setInterval(function() { ms++; if (ms == 100) { ms = 0; s++; if (s == 60) { s = 0; m++; if (m == 60) { m = 0; } mElement.innerText = m < 10 ? '0' + m : m; } sElement.innerText = s < 10 ? '0' + s : s; } msElement.innerText = ms < 10 ? '0' + ms : ms; }, 10); } }; pauseButton.onclick = function() { clearInterval(timer); timer = null; }; resetButton.onclick = function() { clearInterval(timer); timer = null; ms = 0; s = 0; m = 0; msElement.innerText = '00'; sElement.innerText = '00'; mElement.innerText = '00'; };更改代码实现,初始化暂停和复位按钮失效(变灰),开始按钮触发后,暂停和复位按钮生效;复位后,暂停和复位按钮失效
时间: 2023-12-01 20:02:57 浏览: 52
可以通过设置按钮的 `disabled` 属性来实现按钮的禁用和启用。具体更改后的代码如下:
```
const startButton = document.getElementById('start');
const pauseButton = document.getElementById('pause');
const resetButton = document.getElementById('reset');
const msElement = document.getElementById('ms');
const sElement = document.getElementById('s');
const mElement = document.getElementById('m');
let ms = 0;
let s = 0;
let m = 0;
let timer = null;
startButton.onclick = function() {
startButton.disabled = true;
pauseButton.disabled = false;
resetButton.disabled = false;
if (timer == null) {
timer = setInterval(function() {
ms++;
if (ms == 100) {
ms = 0;
s++;
if (s == 60) {
s = 0;
m++;
if (m == 60) {
m = 0;
}
mElement.innerText = m < 10 ? '0' + m : m;
}
sElement.innerText = s < 10 ? '0' + s : s;
}
msElement.innerText = ms < 10 ? '0' + ms : ms;
}, 10);
}
};
pauseButton.onclick = function() {
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = false;
clearInterval(timer);
timer = null;
};
resetButton.onclick = function() {
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = true;
clearInterval(timer);
timer = null;
ms = 0;
s = 0;
m = 0;
msElement.innerText = '00';
sElement.innerText = '00';
mElement.innerText = '00';
};
```
阅读全文