【发布时间】:2018-03-25 12:45:10
【问题描述】:
我需要调用 API 来显示某事的进度。
我创建了一个每 1.5 秒执行一次的服务
主要组件
private getProgress() {
this.progressService.getExportProgress(this.type, this.details.RequestID);
}
Services.ts
public getExportProgress(type: string, requestId: string) {
Observable.interval(1500)
.switchMap(() => this.http.get(this.apiEndpoint + "Definition/" + type + "/Progress/" + requestId))
.map((data) => data.json().Data)
.subscribe(
(data) => {
if (!data.InProgress)
//Stop doing this api call
},
error => this.handleError(error));
}
通话有效,但仍在继续。当进度完成时,我想停止 API 调用(if (!data.InProgress),但我坚持这一点。
if (!data.InProgress) 时如何正确取消订阅这个 observable?
谢谢
【问题讨论】:
标签: angular typescript long-polling