<!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="box"></div>
<button>按钮</button>
<script>
var btn=document.querySelector('button');
var lock=true;// 开关
var a=10;
btn.οnclick=function(){
if(!lock){ //判断开关是否关闭
return; //返回中止
}
lock=false;//关闭开关
var timer= setInterval(function(){
btn.innerHTML=a;
a--;
if(a==0)
{
lock=true;//重新打开开光
clearInterval(timer);
a = 10;
}
},1000)
}
</script>
</body>
</html>
函数的节流(定时器的bug)
最新推荐文章于 2024-08-18 08:10:44 发布