一 防抖
function debounce(fn,delay){
let timer=null;
return function(){
let context=this;
let arg=arguments;
if(timer){
clearTimeout(timer)
}
timer=setTimeout(()=>{
fn.apply(context,arg);
timer=null;
},delay)
}
}
const debounced=debounce(()=>console.log('debounce'),2000);

二 节流
function throttle(fn,delay){
let last=0;
return function(){
let now=new Date();
if(now-last>=delay){
last=now;
fn.apply(this,arguments);
}
}
}
function throttle(fn,delay){
let timer=null;
return function(){
let context=this;
let arg=arguments;
if(!timer){
let timer=setTimeout(()=>{
fn.apply(context,arg);
timer=null;
},delay)
}
}
}
const throttled=throttle(()=>console.log('throttle'),2000);
