【问题标题】:lodash.throttle - throttled method not being called?lodash.throttle - 没有调用节流方法?
【发布时间】:2019-09-26 16:55:39
【问题描述】:

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

import throttle from "lodash.throttle"

const throttledHandleResize = () => { 

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

window.addEventListener("resize", throttledHandleResize);

控制台不记录我的字符串。如果我不尝试用throttle 包装它,该方法有效。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript lodash throttling


    【解决方案1】:

    您正在创建一个返回受限制函数的函数。每次出现resize 时,您都在创建一个新 节流函数。只需使用节流功能:

    import throttle from "lodash.throttle"
    
    const throttledHandleResize = throttle(() => {
            console.log("resizing...");
        }, 200);
    
    window.addEventListener("resize", throttledHandleResize);
    

    【讨论】:

      猜你喜欢
      • 2015-07-09
      • 2020-02-13
      • 1970-01-01
      • 2020-12-03
      • 2023-03-09
      • 2012-03-13
      • 2012-08-25
      • 2015-01-21
      相关资源
      最近更新 更多