【问题标题】:lodash throttle nor debounce is working in Angular 5 projectlodash throttle nor debounce 在 Angular 5 项目中工作
【发布时间】:2018-09-27 07:06:22
【问题描述】:

我正在尝试在 Angular 5 项目中使用 lodash 油门和去抖动功能,但它似乎没有按预期工作。

行为是传递给任一函数的函数参数从不执行。

以油门为例,我使用以下方法导入它:

import throttle = require('lodash/throttle');

然后,在任何方法中,我都有以下内容:

throttle(this.testFunction, 100);

我也试过了:

throttle(() => {
          this.testFunction();
        }, 1000);

testFunction 如下:

  public testFunction() {
    console.log('test function!@!!@!');
  }

任何帮助表示赞赏!

【问题讨论】:

    标签: javascript angular typescript lodash throttling


    【解决方案1】:

    throttle 不调用函数。它返回一个新函数,该函数在调用时可确保您传递给 throttle 的真正函数每 x 次最多调用一次:

    所以,如果你这样做:

    throttle(func, 100);
    

    什么都没有发生。你必须这样做:

    let throttledFunc = throttle(func, 100);
    

    而且您必须调用throttledFunc 而不是functhrottledFunc 将检查您是否在至少 100 毫秒内没有调用该函数

    所以,如果你这样做:

    setInterval(throttledFunc, 50); // execute every 50 ms.
    

    func 只会每 100 毫秒调用一次,而不是每 50 毫秒调用一次。

    【讨论】:

    • 我不需要 setInterval,所以我只使用了throttledFunc.call()
    猜你喜欢
    • 1970-01-01
    • 2019-11-09
    • 2019-07-20
    • 2018-05-28
    • 2013-11-17
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多