防抖与节流

94 阅读1分钟
//防抖 debounce
function debounce(fn,delay){
    var timer = null;
    return (...args)=>{
        clearTimeout(timer);
        timer = setTimeout(()=>{
            fn.apply(this,args);
        },delay);
    }
}
//节流  throttle
function throttle(fn,delay){
    var timer = null;
    return (...args)=>{
        if(!timer){
            fn.apply(this,args);
            timer = setTimeout(()=>{
                timer = null;
            },delay)
        }
    }
}