fac*_*urn 1 polling typescript angular
我执行了以下服务,
export class UploadPollingService {
constructor(private http: Http, private appConfig: AppConfigService) { }
checkUploadInfo(term: string, ): Observable<Event[]> {
return this.http
.get(this.appConfig.getAPIUrl() + `/checkStatus?processId=${term}`)
.map(this.extractData)
.catch(this.handleErrors);
}
Run Code Online (Sandbox Code Playgroud)
我在组件内部使用它,我想每1秒调用一次此服务并检查状态,基本上是进行轮询。怎么做?
this.uploadPollingService.checkUploadInfo()
Run Code Online (Sandbox Code Playgroud)
您必须timeinterval像这样使用您的服务方法
ngOnInit(){
this.interval = setInterval(() => {
this.checkUpdate();
}, 1000);
}
ngOnDestroy() {
if (this.interval) {
clearInterval(this.interval);
}
}
checkUpdate(){
this.uploadPollingService.checkUploadInfo()
.subscribe(res => {
console.log(res, "Response here");
},
err => {
console.log(err);
})
}
....
export class UploadPollingService {
constructor(private http: Http, private appConfig: AppConfigService) { }
checkUploadInfo(term?: string): Observable<Event[]> {
return this.http
.get(this.appConfig.getAPIUrl() + `/checkStatus?processId=${term}`)
.map( res => {
return [{ status: res.status, json: res.json() }]
})
.catch(this.handleErrors);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5380 次 |
| 最近记录: |