节流与防抖
作者:互联网
1、节流
创建一个div
<div class="boss">1</div>
js控制
var boss=document.querySelector(".boss");
/*获取*/
var num=parseInt(boss.innerHTML);
/*转类型*/
var bossTime=null;
var bol=true;
boss.onclick=function () {
if(bol){
bol=false;
bossTime=setTimeout(function () {
num++;
boss.innerHTML=num;
bol=true;
},1000)
}
}
2、防抖
boss.onclick = function(){
clearTimeout(bossTime);
if(bol){
num++;
boss.innerHTML = num;
bol=false;
}else{
bossTime = setTimeout(function(){
bol = true;
},1000)
}
}
标签:bossTime,function,防抖,节流,boss,num,bol,var 来源: https://blog.csdn.net/m0_54230910/article/details/120643503