简体   繁体   English

lodash节流阀或去抖在Angular 5项目中工作

[英]lodash throttle nor debounce is working in Angular 5 project

I am trying to use lodash throttle and debounce functions in angular 5 project, however it doesn't seem to work as expected. 我正在尝试在angular 5项目中使用lodash节流阀和去抖动功能,但是它似乎没有按预期工作。

Behavior is that the function parameter that is passed to either of the functions is never executed. 行为是传递给两个函数的函数参数从不执行。

For example with throttle, I am importing it using the following approach: 例如对于节流阀,我正在使用以下方法导入它:

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

Then, inside any method, I have the following: 然后,在任何方法中,我都有以下内容:

throttle(this.testFunction, 100);

I have also tried: 我也尝试过:

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

testFunction is just the following: testFunction如下:

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

Any help appreciated! 任何帮助表示赞赏!

throttle does not call a function. throttle不调用函数。 It returns a new function, that, when called, ensures that the true function you passed to throttle is called, at maximum, once every x time: 它返回一个新函数,该函数在被调用时确保确保您传递给throttle的真正函数最多每x次被调用一次:

So, if you do: 因此,如果您这样做:

throttle(func, 100);

Nothing happens. 什么都没发生。 You must do: 您必须做:

let throttledFunc = throttle(func, 100);

And you must call throttledFunc instead of func . 而且您必须调用throttledFunc func而不是func throttledFunc will check that you haven't called the function in at least 100 ms throttledFunc将检查您至少有100毫秒没有调用该函数

So, if you do: 因此,如果您这样做:

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

func will be only called every 100 ms, not every 50. func将仅每100毫秒调用一次,而不是每50毫秒调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM