logo头像

流莹离|拼命往前,仗剑天涯

节流和防抖

节流 throttle(译音:sralte)

防抖 debounce(译音:dibeiaos)

简化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function debounce(fn,delay){
let timer = null
return (...args)=>{
clearTimeout(timer)
timer = setTimeout(()=>{
fn.apply(this,args)
},delay)
}
}
function domClick(){
debounce((args)=>{
console.log(args)
},2000)
}

按钮点击节流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 按钮 点击节流
*/
export const stopClick = function (name,callBack,time=1000,msg=`请等待1s后再次点击`){
return new Promise((resolve,reject)=>{
if(name){
console.log(`${msg} >>>>>>>>>`)
return false
}
callBack(true)
setTimeout(()=>{
callBack(false)
},time)
resolve()
})
}