<script> var timer = null; var ms = 0, s = 0, m = 0; var msElement = document.getElementById('ms'); var sElement = document.getElementById('s'); var mElement = document.getElementById('m'); var startButton = document.getElementById('start'); var pauseButton = document.getElementById('pause'); var resetButton = document.getElementById('reset'); 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'; }; </script> 补全JS代码实现:初始化:暂停-复位按钮失效;复位后,暂停按钮失效
时间: 2024-01-11 20:02:00 浏览: 116
可以在复位按钮的点击事件中添加一个判断,如果当前定时器不为null,则暂停按钮失效,代码如下:
```
resetButton.onclick = function() {
clearInterval(timer);
timer = null;
ms = 0;
s = 0;
m = 0;
msElement.innerText = '00';
sElement.innerText = '00';
mElement.innerText = '00';
pauseButton.disabled = true; // 暂停按钮失效
if (timer == null) {
startButton.disabled = false; // 启动按钮生效
}
};
```
在启动按钮的点击事件中添加一个判断,如果当前定时器为null,则启动按钮失效,代码如下:
```
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);
}
startButton.disabled = true; // 启动按钮失效
pauseButton.disabled = false; // 暂停按钮生效
};
```
阅读全文