javascript编写一个脚本,当您单击“开始”按钮时运行并将时间倒计时到 0 分 0 秒, 然后显示用户设置的提醒文本,并将页面的背景色更改为用户指定的颜色。 倒计时分钟和秒应每秒显示在页面上的相应输入字段中(带有标识符和minsec); 当时间变为 0 分 0 秒时,倒计时应停止; 有必要实现重新启动倒计时的功能(在这种情况下,您需要重置页面的背景颜色); 如果倒计时已经开始,“开始”按钮不应响应按下; 按下“暂停”按钮,倒计时应停止; 通过按“停止”按钮,倒计时应停止,字段值应重置为0。向页面添加其他按钮和进度条元素。 用户在字段中设置输入分钟和秒的时间,单击按钮后,指标值重置为 0, 经过指定的时间后,指标值应达到 100 值。更改指示器集的状态,例如,每秒 1 次。。选项中的按钮不应响应重复按下。
时间: 2024-02-18 12:03:39 浏览: 75
好的,这是一项比较复杂的任务,需要使用 JavaScript 和 HTML/CSS 来完成。以下是一个可能的解决方案:
HTML:
```
<div>
<label for="minutes">Minutes:</label>
<input type="number" id="minutes" min="0" max="60" value="0">
</div>
<div>
<label for="seconds">Seconds:</label>
<input type="number" id="seconds" min="0" max="60" value="0">
</div>
<button id="start">Start</button>
<button id="pause">Pause</button>
<button id="stop">Stop</button>
<div>
<label for="minsec">Time remaining:</label>
<input type="text" id="minsec" value="0:00" readonly>
</div>
<div>
<label for="reminder">Reminder:</label>
<input type="text" id="reminder">
</div>
<div>
<label for="color">Background color:</label>
<input type="color" id="color" value="#ffffff">
</div>
<div>
<progress id="progress" value="0" max="100"></progress>
</div>
```
JavaScript:
```
const minutesField = document.getElementById('minutes');
const secondsField = document.getElementById('seconds');
const startBtn = document.getElementById('start');
const pauseBtn = document.getElementById('pause');
const stopBtn = document.getElementById('stop');
const minsecField = document.getElementById('minsec');
const reminderField = document.getElementById('reminder');
const colorField = document.getElementById('color');
const progressField = document.getElementById('progress');
let countdownInterval = null;
let countdownTime = 0;
let countdownPaused = false;
let countdownStopped = false;
let countdownStartTime = 0;
let countdownEndTime = 0;
let countdownReminder = '';
let countdownColor = '';
function startCountdown() {
if (countdownInterval) {
return;
}
const minutes = parseInt(minutesField.value);
const seconds = parseInt(secondsField.value);
if (isNaN(minutes) || isNaN(seconds)) {
return;
}
countdownTime = minutes * 60 + seconds;
countdownPaused = false;
countdownStopped = false;
countdownStartTime = Date.now();
countdownEndTime = countdownStartTime + countdownTime * 1000;
countdownReminder = reminderField.value;
countdownColor = colorField.value;
startBtn.disabled = true;
pauseBtn.disabled = false;
stopBtn.disabled = false;
countdownInterval = setInterval(updateCountdown, 1000);
}
function pauseCountdown() {
if (!countdownInterval) {
return;
}
if (countdownPaused) {
countdownPaused = false;
countdownStartTime = Date.now();
countdownEndTime = countdownStartTime + countdownTime * 1000;
countdownInterval = setInterval(updateCountdown, 1000);
} else {
countdownPaused = true;
clearInterval(countdownInterval);
countdownInterval = null;
}
}
function stopCountdown() {
if (!countdownInterval) {
return;
}
countdownStopped = true;
clearInterval(countdownInterval);
countdownInterval = null;
countdownTime = 0;
countdownPaused = false;
countdownStartTime = 0;
countdownEndTime = 0;
countdownReminder = '';
countdownColor = '';
startBtn.disabled = false;
pauseBtn.disabled = true;
stopBtn.disabled = true;
minsecField.value = '0:00';
progressField.value = 0;
document.body.style.backgroundColor = '';
}
function updateCountdown() {
const now = Date.now();
const remaining = Math.max(0, countdownEndTime - now);
const minutes = Math.floor(remaining / 60000);
const seconds = Math.floor(remaining / 1000) % 60;
minsecField.value = `${minutes}:${seconds.toString().padStart(2, '0')}`;
progressField.value = 100 - remaining / countdownTime / 10;
if (remaining <= 0) {
clearInterval(countdownInterval);
countdownInterval = null;
minsecField.value = '0:00';
progressField.value = 100;
document.body.style.backgroundColor = countdownColor;
alert(countdownReminder);
}
}
startBtn.addEventListener('click', startCountdown);
pauseBtn.addEventListener('click', pauseCountdown);
stopBtn.addEventListener('click', stopCountdown);
```
这个脚本使用了一些常见的 JavaScript 技术,如事件监听器、定时器和 DOM 操作。当用户单击“开始”按钮时,脚本会读取分钟和秒输入字段的值,并将其转换为秒数。然后,它会启动一个倒计时定时器,并在每秒钟更新剩余时间和进度条的值。当倒计时结束时,脚本会更改页面的背景颜色,并显示用户设置的提醒文本。
用户可以通过单击“暂停”按钮来暂停倒计时,再次单击该按钮则可以继续倒计时。用户可以通过单击“停止”按钮来停止倒计时,重新设置所有字段的值,并将页面的背景颜色重置为默认值。
阅读全文