Liu*_*s94 1 javascript throttling typescript lodash angular
我正在尝试在 angular 5 项目中使用 lodash 油门和去抖动功能,但它似乎没有按预期工作。
行为是永远不会执行传递给任何一个函数的函数参数。
例如使用油门,我使用以下方法导入它:
import throttle = require('lodash/throttle');
Run Code Online (Sandbox Code Playgroud)
然后,在任何方法中,我都有以下内容:
throttle(this.testFunction, 100);
Run Code Online (Sandbox Code Playgroud)
我也试过:
throttle(() => {
this.testFunction();
}, 1000);
Run Code Online (Sandbox Code Playgroud)
testFunction 只是以下内容:
public testFunction() {
console.log('test function!@!!@!');
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
throttle不调用函数。它返回一个新函数,该函数在调用时确保您传递给的真正函数throttle最多每 x 次被调用一次:
所以,如果你这样做:
throttle(func, 100);
Run Code Online (Sandbox Code Playgroud)
没发生什么事。你必须这样做:
let throttledFunc = throttle(func, 100);
Run Code Online (Sandbox Code Playgroud)
你必须调用throttledFunc而不是func. throttledFunc将检查您是否在至少 100 毫秒内没有调用该函数
所以,如果你这样做:
setInterval(throttledFunc, 50); // execute every 50 ms.
Run Code Online (Sandbox Code Playgroud)
func 只会每 100 毫秒调用一次,而不是每 50 毫秒调用一次。
| 归档时间: |
|
| 查看次数: |
2036 次 |
| 最近记录: |