In my Angular2 application there are two components for form view and graph view. In the form, there is a spinner and checkbox for auto refresh handle.
Form Component html
<div class="refresh-spinner">
<my-checkbox
[selected]=autoRefresh
[disabled]="false"
[id]="'autoRefresh'"
[name]="'Auto Refresh Every'"
[code]="'autoRefresh'"
(status)="checkAutoRefresh($event)">
</my-checkbox>
<my-spinner [title]="''"
[category]="'duration'"
[maxCount]=maxRefreshTime
[minCount]=minRefreshTime
[value]=minRefreshTime //default refresh value is 1 min
[editable]="true"
(count)="getAutoRefreshInterval($event)">
</my-spinner>
<span class="post__text"> Mins</span>
</div>
Form Component ts
// form view emits selected criteria
@Output() criteria = new EventEmitter<any>();
checkAutoRefresh(ele) {
this.autoRefresh = ele.selected;
localStorage.setItem('autoRefresh', ele.selected);
}
getAutoRefreshInterval(interval) {
localStorage.setItem('refreshInterval', interval.value);
}
Refresh interval and checkbox value (autoRefresh true/fasle) are set is local storage on spinner event and checkbox select event.
Graph components ts
alive: boolean; // used to unsubscribe from the IntervalObservable when OnDestroy is called.
@Input() criteria: FilteringCriteria;
constructor(private element: ElementRef, private myService: MyService) {
this.alive = true;
}
ngOnInit() {
let interval: number = JSON.parse(localStorage.getItem('refreshInterval'));
console.log(Date());
this.getStatistics();
IntervalObservable.create(interval * 60 * 1000)
.takeWhile(() => this.alive)
.subscribe((e) => {
// console.log(e)
if (JSON.parse(localStorage.getItem('autoRefresh'))) {
console.log(Date());
this.getStatistics();
}
});
}
ngOnDestroy() {
this.alive = false;
}
These form view and graph view are used in main component as below.
<search-criteria (criteria)="emitCriteria($event)"></search-criteria> //filteringCriteria emits from here
<ng-template ngFor let-result [ngForOf]="servers" let-i="index">
<my-grid-item row [class]="'graph--view'"
[colspan]="4">
<graph-view [criteria]="filteringCriteria"></graph-view>
</my-grid-item>
</ng-template>
Two Questions:
1. Once I check auto refresh checkbox graphs are refresh in 1 minute. But time interval is calculating from the time component is initialized not from the time the checkbox is selected.
2 If I change the value of the spinner (from 1 min to 2 min) local storage value is changed to new value 2. But graphs are refreshing in 1 min time intervals. But if I hit on form done button, then the graphs are refreshing in new time interval(2 min).
Any suggestions are appreciated.
Thank You!