JS函数防抖节流

函数防抖debounce:(多次操作合并为一次)

1 function debounce (fn,delay) {
2     var timer = null;
3     return function (e) {
4         clearTimeout(timer);
5         timer = setTimeout(() => {
6             fn.apply(this,argument);
7         },delay)
8     }
9 }

函数节流throttle:(一定时间内只触发一次)

function throttle(fn,delay) {
    let canRun = true;
    return function () {
        if(!canRun) return;
        canRun = false;
        setTimeout(() => {
            fn.apply(this,arguments);
            canRun = true;
        },delay);
    }
}

 

posted @ 2022-03-07 16:34  给我一个debug  阅读(29)  评论(0)    收藏  举报