定时器
·开启定时器
setInterval(show,1000) 间隔型 不断重复
setTimeout(show,1000) 延时型 只执行一次
·停止定时器
clearInterval
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function show() {
alert("a");
}
window.onload = function() {
var oBtn1 = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
var timer = null;
oBtn1.onclick = function () { //开启定时器
timer = setInterval(show,1000);
}
oBtn2.onclick = function () { //开启定时器
clearInterval(timer);
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="开启定时器">
<input id="btn2" type="button" value="关闭定时器">
</body>
</html>
clearTimeout