我如何每1秒钟致电服务并使用angular2检查响应? [英] How can i call service every 1 second and check the response with angular2?
本文介绍了我如何每1秒钟致电服务并使用angular2检查响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实现了以下服务
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);
}
我正在组件内部使用它,我想每1秒调用一次此服务并检查状态,基本上是进行轮询.怎么办?
I am using this inside a component, and i want to call this service every 1 second and check status, basically do a polling. how to do?
this.uploadPollingService.checkUploadInfo()
推荐答案
您必须像这样在timeinterval
中使用服务方法
you have to use your service method within timeinterval
like this
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);
}
这篇关于我如何每1秒钟致电服务并使用angular2检查响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文