I want to run my code as soon as it gets into my component and repeat the logic every 5 seconds
Here is my code, I used the interval of rxjs
ngOnInit() {
const ticker = interval(5000);
ticker.subscribe(() => {
this.calculateTime();
});
}
But the issue here is that for the code runs
1st time at 5s
2nd time at 10s
But I want it to run as
1st time at 0s
2nd time at 5s
3rs time at 10s
This is how I tacked:
ngOnInit() {
const ticker = interval(5000);
this.calculateTime();
ticker.subscribe(() => {
this.calculateTime();
});
}
If you notice I called this.calculateTime();
twice once before ticker and 2nd inside ticker.
Is there a better solution using
interval
? If not may be an alternative tointerval