【问题标题】:lodash debounce cancel() not stopping the debounce executionlodash debounce cancel() 不停止 debounce 执行
【发布时间】:2020-08-10 04:54:13
【问题描述】:

我编写了一个 Angular 应用程序,我在其中对每个 modelChange 事件进行 http 调用。我使用了 lodash _.debounce()。问题是我无法在第一次成功执行 debounce 后取消这些调用。

  modelChangeEvent(item):void {
   const _this = this;
   let debounceObj = _.debounce(function(){
     _this.makeHttpCall(item);
     debounceObj.cancel();
    },600);
  debounceObj();
  }

https://lodash.com/docs/4.17.15#debounce

【问题讨论】:

  • 为什么不用rxjs?它带有 Angular,并且已经有几十个类似的功能。

标签: angular typescript lodash debounce angular-event-emitter


【解决方案1】:

使用 Rxjs 实现 debounceTime()distinctUntilChanged()

detectInput:any = new Subject();

ngOnInit(){
  this.detectInput.pipe(debounceTime(400),distinctUntilChanged()).subscribe(value=>{
    this.makeHttpCall(value)
  })
}

modelChangeEvent(item):void {
 this.detectInput.next(item);
}

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2019-04-08
    • 2019-11-09
    • 2018-05-28
    • 2021-02-25
    • 2019-07-20
    • 2018-10-11
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多