簡體   English   中英

lodash節流閥或去抖在Angular 5項目中工作

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

我正在嘗試在angular 5項目中使用lodash節流閥和去抖動功能,但是它似乎沒有按預期工作。

行為是傳遞給兩個函數的函數參數從不執行。

例如對於節流閥,我正在使用以下方法導入它:

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

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

throttle(this.testFunction, 100);

我也嘗試過:

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

testFunction如下:

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

任何幫助表示贊賞!

throttle不調用函數。 它返回一個新函數,該函數在被調用時確保確保您傳遞給throttle的真正函數最多每x次被調用一次:

因此,如果您這樣做:

throttle(func, 100);

什么都沒發生。 您必須做:

let throttledFunc = throttle(func, 100);

而且您必須調用throttledFunc func而不是func throttledFunc將檢查您至少有100毫秒沒有調用該函數

因此,如果您這樣做:

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

func將僅每100毫秒調用一次,而不是每50毫秒調用一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM