getStats() {
let statsLS = localStorage.getItem('stats');
let slt = +localStorage.getItem('slt');
let date = new Date();
let time = date.getTime();
if(statsLS === null || slt < time) {
this.http.get('/server/api/statsService').subscribe((data: any) => {
this.stats = data;
let date = new Date();
let time = date.getTime()+60000+'';
localStorage.setItem('stats',JSON.stringify(this.stats));
localStorage.setItem('slt',time);
return this.stats;
});
} else {
return JSON.parse(statsLS);
}
}
}
ngOnInit() {
this.statsService.getStats().subscribe((data: any) => console.log(data));
}
getStats()
{
return new Observable<any>((observer) => {
let statsLS = localStorage.getItem('stats');
let slt = +localStorage.getItem('slt');
let date = new Date();
let time = date.getTime();
if (statsLS === null || slt < time) {
this.http.get('/server/api/statsService').subscribe((data: any) => {
this.stats = data;
let date = new Date();
let time = date.getTime() + 60000 + '';
localStorage.setItem('stats', JSON.stringify(this.stats));
localStorage.setItem('slt', time);
observer.next(this.stats);
});
} else {
observer.next(JSON.parse(statsLS));
}
});
}