In my code, I have a method that is called every 10000 times. Now, I want to modify this so that the function getAllNotificationsActed0()
is invoked every 10 seconds. If the data does not arrive within this interval, I do not want the function to be called again. Only when the data arrives within 10 seconds should the function be executed; otherwise, it should wait.
service.ts
public NotifGetAllActedNoActive(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.NotifGetAllActedNoActive), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
component.ts
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
this.getAllNotificationsActed0();
});
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive().subscribe(notification0 => {
this.notification0 = notification0;
if (this.isSortedByDate) {
this.sortbydate();
}
});
}
Any suggestions for improvement?