仅用于 1 个异步调用的 setTimeout

setTimeout for only 1 asynchronous call

我们如何将 setTimeout 仅用于 1 个异步调用? .我想在从数据服务调用 GetData 之前设置超时,但 setTimeout 应该只用于 1 个异步调用。

有什么想法吗?谢谢。

#html代码

 <app-table-multi-sort (dataServiceEvent)="dataServiceEvent($event)"></app-table-multi-sort>

#ts 代码

dataServiceEvent(item) {
    this.table = item;
    if (this.table) {
      setTimeout(()=>{
        this._GetData()
        },500); 
    }
  }

private GetData() {
    this.isLoading = true;
    this._brokerOpinionOfValueService
      .getAllWAGBOVs(
        this.accountId,
        this.table.pageIndex + 1,
        this.table.pageSize,
        this.searchInput.nativeElement.value,
        this.table.sortParams,
        this.table.sortDirs
      )
      .pipe(finalize(() => (this.isLoading = false)))
      .subscribe({
        error: (err) => this._notificationService.showError(err),
        next: (res) => {
          
        },
        complete: noop,
      });
  }

可以这样做:-

getDataSubject: Subject = new Subject();

ngOnInit() {
  this.getDataSubject.pipe(
    scan((acc, curr) => acc + 1, 0),
    mergeMap(count => {
        return iif(() => count < 2, this._GetData().pipe(delay(500)), this.getData())
    })
  ).subscribe();
}

dataServiceEvent(item) {
  this.getDataSubject.next();
}