节流函数

多次调用函数,设定两次函数调用的最小时间间隔。

function throttle(fn, timeout = 1000) {
    let timer;
    return (...args) => {
        if (timer) {
            return;
        }
        fn.apply(this, args);
        timer = setTimeout(() => {
            timer = (void 0);
        }, timeout);
    }
}

最后更新于