debounce 和 throttle

2020-10-28  本文已影响0人  bestCindy
const debounce = (fn, time) => {
    let timeout = null;
    return function() {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            fn.apply(this, arguments);
        }, time);
    }
}
const throttle = (fn, time) => {
    let flag = true;
    return function() {
        if (!flag) return;
        flag = false;
        setTimeout(() => {
            fn.apply(this, arguments);
            flag = true;
        },time);
    }
}
上一篇下一篇

猜你喜欢

热点阅读