js制作计时器,含有开始计时,暂停计时,停止计时
时间: 2024-01-31 07:09:05 浏览: 35
以下是一个简单的JavaScript计时器,包含开始计时,暂停计时和停止计时功能。你可以将以下代码复制到HTML文件中并在浏览器中运行,以查看计时器的效果。
```html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript计时器</title>
</head>
<body>
<h1>JavaScript计时器</h1>
<p id="timer">00:00:00</p>
<button onclick="startTimer()">开始计时</button>
<button onclick="pauseTimer()">暂停计时</button>
<button onclick="stopTimer()">停止计时</button>
<script>
var timer = document.getElementById("timer");
var interval;
var seconds = 0, minutes = 0, hours = 0;
function startTimer() {
interval = setInterval(function() {
timer.innerHTML = formatTime(hours) + ":" + formatTime(minutes) + ":" + formatTime(seconds);
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours++;
}
}, 1000);
}
function pauseTimer() {
clearInterval(interval);
}
function stopTimer() {
clearInterval(interval);
seconds = 0;
minutes = 0;
hours = 0;
timer.innerHTML = "00:00:00";
}
function formatTime(time) {
if (time < 10) {
time = "0" + time;
}
return time;
}
</script>
</body>
</html>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)