我正在调用API从数据库中获取数据。我想在5秒或未连接Internet后未加载数据时显示消息。如何添加计时器从调用API开始,在未加载数据后5秒显示消息后?
home.ts
ngOnInit() {
this._myservice.myservice(this.taskdesc).subscribe(data => {
this.displaydata = data;
});
}
homeservice.ts
myservice(taskdesc) {
console.log(">>" + this.loggedinuser);
let params = '&taskdesc=' + taskdesc;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this._http.post(this._url + this.loggedinuser+this._urlend+this.loggedinuser, params, {headers: headers})
.map((response: Response) => response.json());
}
请建议。
预先感谢您。
您可以从Observable
中使用.timeout(5000)
return this.http.post()
.timeout(5000, new Error('Your Message'))
.map((response: Response) => response.json());