我试图每秒调用我的API。我不知道该怎么做。
准时来电是可以的:
loadRequest(payload: any): Observable<RequestTask[]> {
const headers = new Headers({'Authorization': localStorage.getItem('authorizationToken')});
const options = new RequestOptions({headers: headers});
const requestId = payload;
const url = '/api/requests/'+ requestId +'/events'
return this.http
.get(url, options)
.map(response => {
const requests = response.json();
console.log("Request API", requests)
return requests.map(request => {
return new RequestTask(request)
});
})
.catch(this.handleError);
}
我的每秒呼叫测试
loadRequest(payload: any): Observable<RequestTask[]> {
const headers = new Headers({'Authorization': localStorage.getItem('authorizationToken')});
const options = new RequestOptions({headers: headers});
const requestId = payload;
const url = '/api/requests/'+ requestId +'/events'
return Observable.interval(500)
.flatMap(()=> {
return this.http
.get(url, options)
.map(response => {
const request = response.json();
return request.map(request => {
return new RequestTask(request)
});
}).catch(this.handleError);
})
}
我认为我的错误来自Observable,但我看不出如何解决它