<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
页面上有输出内容的地方
有两个按钮 开始 暂停
1.页面上的时钟会动
2.开始暂停
-->
<script>
var timer;
window.onload = function(){
//1.1页面加载后才有div
//1.2 设置定时器 定时调用某个函数
timer = setInterval(show , 1000);
//1.4 手动调用一次show 初始化第一次的数据
show();
}
function show(){
//1.3 获得div设置时间 需要覆盖效果
document.getElementById("myDiv").innerHTML = new Date().toLocaleString();
}
//开始-->> 开启一个定时器
function startFn(){
timer = setInterval(show , 1000);
}
//暂停 -->> 清除定时器即可
function stopFn(){
clearInterval(timer);
}
</script>
</head>
<body>
<div id="myDiv"></div>
<input type="button" value="开始" onclick="startFn()">
<input type="button" value="停止" onclick="stopFn()">
</body>
</html>