【发布时间】:2019-02-16 01:50:51
【问题描述】:
我每 10000 次调用一个方法。
我想让这个函数getAllNotificationsActed0() 每 10 秒调用一次,如果数据不在这个时间间隔内,就不要再调用这个函数。如果10秒内收到数据则调用该函数,如果10秒内没有收到数据则不调用而是等待。
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);
});
}
});
}
组件.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();
}
});
}
有什么想法吗?
【问题讨论】:
标签: angular typescript subscription