如何使用Observer每秒向服务器发送请求?

时间:2018-09-03 22:57:02

标签: angular rxjs angular5

只需将此代码包装到计时器中?

this.requestMethods.CallNotaryRequest(data).subscribe((response: ICallNotaryResponse) => {
    this.requestMethods.CallNotaryRequest();// Call again after 1 second
});

还是有另一种方法?

只有在1秒后收到上一个的响应时,我才需要执行相同的请求

我尝试过这种方式:

Observable.interval(3000).subscribe(i => {
  this.askToReadyRecordVideo().subscribe(res => {
  });
});

private askToReadyRecordVideo(): Observable<any> {
    return this.requestMethods.askToReadyRecordVideo({});
}

但是服务器返回错误时仍然存在问题,它仍然发送请求

1 个答案:

答案 0 :(得分:1)

Observable.interval(3000)
    // the error from inner stream(inside `switchMap`) will propagate to outer stream. 
    // Outer stream will complete.
    .pipe(switchMap(()=>this.askToReadyRecordVideo()))
    .subscribe(console.log);

private askToReadyRecordVideo(): Observable<any> {
    return this.requestMethods.askToReadyRecordVideo({});
}