<!DOCTYPE html>
<html>
<head>
<title>电子时钟</title>
</head>
<body>
<h1 id="clock"></h1>
<button onclick="toggleClock()">停止/启动</button>
<script>
var clockInterval;
function startClock() {
clockInterval = setInterval(updateClock, 1000);
}
function stopClock() {
clearInterval(clockInterval);
}
function updateClock() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeString =
hours.toString().padStart(2, "0") +
":" +
minutes.toString().padStart(2, "0") +
":" +
seconds.toString().padStart(2, "0");
document.getElementById("clock").textContent = timeString;
}
function toggleClock() {
if (clockInterval) {
stopClock();
} else {
startClock();
}
}
startClock();
</script>
</body>
</html>
05-26
1939
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-21
01-11
1809
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交