防抖封装
Function antiShake(fn,wait){
let timeout = null
return args => {
if(timeout) clearTimeout(timeout)
Timeout = setTimeout(fn,wait);
}
}
Function demo(){
console.log(‘发起请求’)
}
// 防抖— 将多次操作变成一次
Let telInput = document.querySelector(‘input’);
telInput.addEventListener(‘input’, antiShake(demo,2000)
节流 — 一定时间内只调用一次函数
应用场景: 1.提交表单。2. 高频监听事件
funtion throttle(event, time){
let timer = null
return function(){
if(!timer){
timer = setTimeout(() => {
event();
timer = null;
},time)
}
}
}