<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="time"></div>
<!-- 设置两个按钮,可以控制时间暂停和继续 -->
<input type="button" value="暂停" onclick="stop()">
<input type="button" value="继续" onclick="goOn()">
</body>
<script>
function getTime() {
var now = new Date;
document.getElementById("time").innerText = now.toLocaleString();
}
//设置一个定时器,一秒执行一次
var id = window.setInterval(getTime, 1000);
function stop() {
window.clearInterval(id);
}
function goOn() {
id = window.setInterval(getTime, 1000);
}
</script>
</html>