lodash.throttle - 未调用节流方法?

lodash.throttle - throttled method not being called?

我有一个事件侦听器,我正在尝试用 lodash.throttle:

包装它
import throttle from "lodash.throttle"

const throttledHandleResize = () => { 

    return(throttle(() => {
        console.log("resizing...");
    }, 200));
};

window.addEventListener("resize", throttledHandleResize);

控制台没有记录我的字符串。如果我不尝试用 throttle.

包装它,则该方法有效

如有任何帮助,我们将不胜感激!

您正在创建一个 returns 节流函数。每次 resize 发生时,您都会创建一个 new 节流函数。只需使用节流功能:

import throttle from "lodash.throttle"

const throttledHandleResize = throttle(() => {
        console.log("resizing...");
    }, 200);

window.addEventListener("resize", throttledHandleResize);