JS实现简单的节流和防抖函数

-节流函数

function throttle(fn, wait) {
    let sTime = 0, eTime;
    return () => {
        eTime = Date.now();
        if(eTime - sTime < wait) return;
        fn();
        sTime = eTime;
    }
}

-防抖函数

funtion debounce(fn, wait) {
    let timer = null;
    return () => {
        clearTimeout(timer);
        timer = setTimeout(fn, wait)
    }
}

你可能感兴趣的:(JS实现简单的节流和防抖函数)