用html5写一个高考倒计时的页面,可以手动开始计时和停止计时
时间: 2024-05-07 17:17:38 浏览: 8
好的,可以的。首先,我们需要创建一个 HTML 文件,如下所示:
```html
<!DOCTYPE html>
<html>
<head>
<title>高考倒计时页面</title>
</head>
<body>
<div id="countdown">00:00:00</div>
<button onclick="startCountdown()">开始计时</button>
<button onclick="stopCountdown()">停止计时</button>
<script>
var countdownTimer;
var startTime;
var currentTime;
var timeDiff;
function displayTime() {
currentTime = new Date();
timeDiff = Math.floor((currentTime - startTime) / 1000);
var hours = Math.floor(timeDiff / 3600);
var minutes = Math.floor((timeDiff - (hours * 3600)) / 60);
var seconds = timeDiff - (hours * 3600) - (minutes * 60);
document.getElementById("countdown").innerHTML = (("0" + hours).slice(-2) + ":" + ("0" + minutes).slice(-2) + ":" + ("0" + seconds).slice(-2));
}
function startCountdown() {
startTime = new Date();
countdownTimer = setInterval(displayTime, 1000);
}
function stopCountdown() {
clearInterval(countdownTimer);
document.getElementById("countdown").innerHTML = "00:00:00";
}
</script>
</body>
</html>
```
在这个文件中,我们创建了一个 div 元素来显示倒计时,还有两个按钮,一个用于开始计时,一个用于停止计时。当用户点击开始计时按钮时,我们将开始时间存储为一个变量并使用 setInterval 函数来调用 displayTime 函数来更新倒计时。当用户点击停止计时按钮时,我们使用 clearInterval 函数来停止倒计时,并将倒计时文本设置为 "00:00:00"。
希望这能帮助你实现你想要的高考倒计时页面。