计时相关:
1.指定时间之后做一件事
setTimeout(js语句,毫秒)
2.每隔一段时间做一件事
setInterval(js语句,毫秒)
clearInterval(setInterval的变量名)清除重复事件
<!DOCTYPE html>
<html >
<head>
<meta http-equiv="content-Type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
</head>
<body>
<input type="text" >
<button >开始</button>
<button >停止</button>
<script>
var i1Ele = document.getElementById("i1");
var t;
function f() {
var now = new Date();
i1Ele.value = now.toLocaleString();
}
f();
var b1Ele = document.getElementById("b1");
// 点开始
b1Ele.onclick = function (ev) {
if (!t) {
t = setInterval(f, 1000)
}
};
var b2Ele = document.getElementById("b2");
// 点停止
b2Ele.onclick = function (ev) {
clearInterval(t); // 根据id清除定时任务
console.log(t);
t = null;
}
</script>
</body>
</html>