簡體   English   中英

僅用於 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();
}

暫無
暫無

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

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