简体   繁体   English

仅用于 1 个异步调用的 setTimeout

[英]setTimeout for only 1 asynchronous call

How do we use setTimeout for only 1 asynchronous call?我们如何将 setTimeout 仅用于 1 个异步调用? . . I wanna set timeout for a purpose before calling the GetData from the dataservice but setTimeout should only be use for only 1 asynchronous call.我想在从数据服务调用 GetData 之前设置超时,但 setTimeout 应该只用于 1 个异步调用。

Any idea?任何的想法? thanks.谢谢。

#html code #html代码

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

#ts code #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,
      });
  }

Can be done like:-可以这样做:-

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