Javascript lodash throttle或debounce正在Angular 5项目中工作

Javascript lodash throttle或debounce正在Angular 5项目中工作,javascript,angular,typescript,lodash,throttling,Javascript,Angular,Typescript,Lodash,Throttling,我试图在angular 5项目中使用lodash节流和去盎司功能,但它似乎并没有像预期的那样工作 行为是传递给任何一个函数的函数参数永远不会执行 例如,对于油门,我使用以下方法导入它: import throttle = require('lodash/throttle'); 然后,在任何方法中,我都有以下内容: throttle(this.testFunction, 100); public testFunction() { console.log('test function

我试图在angular 5项目中使用lodash节流和去盎司功能,但它似乎并没有像预期的那样工作

行为是传递给任何一个函数的函数参数永远不会执行

例如,对于油门,我使用以下方法导入它:

import throttle = require('lodash/throttle');
然后,在任何方法中,我都有以下内容:

throttle(this.testFunction, 100);
  public testFunction() {
    console.log('test function!@!!@!');
  }
我也尝试过:

throttle(() => {
          this.testFunction();
        }, 1000);
testFunction如下所示:

throttle(this.testFunction, 100);
  public testFunction() {
    console.log('test function!@!!@!');
  }
感谢您的帮助

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

因此,如果你这样做:

throttle(func, 100);
setInterval(throttledFunc, 50); // execute every 50 ms.
什么也没发生。你必须做到:

let throttledFunc = throttle(func, 100);
您必须调用throttledFunc而不是func。throttledFunc将检查您是否在至少100毫秒内未调用该函数

因此,如果你这样做:

throttle(func, 100);
setInterval(throttledFunc, 50); // execute every 50 ms.
func将仅每100毫秒调用一次,而不是每50毫秒调用一次。

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

因此,如果你这样做:

throttle(func, 100);
setInterval(throttledFunc, 50); // execute every 50 ms.
什么也没发生。你必须做到:

let throttledFunc = throttle(func, 100);
您必须调用throttledFunc而不是func。throttledFunc将检查您是否在至少100毫秒内未调用该函数

因此,如果你这样做:

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

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

我不需要设置间隔,所以我只使用了throttledFunc.call。我不需要设置间隔,所以我只使用throttledFunc.call。