Is there any way to fire a function every X seconds or minutes in Angular 2+?
For example, what if I want to lauch a myService.refreshloginStatus()
function from my main app.component
every 60 seconds?
I found this question searching on the internet but I have no idea if it could work also for this and how to implement that in Angular...Can someone provide me a concrete usage of this in Angular?
PS: let me know in the comments if in your opinion this question could be a duplicate of the linked one and I have to remove it
You can use setInterval()
function
setInterval(()=> { this.myFunc() }, timeIntevalSeconds * 1000);
const reloadInterval = 60;
timer(0, reloadInterval).pipe(
mergeMap(_ => this.myService.myHttpCall())
).subscribe()
That's to answer your question. But honnestly I do not think that's a good idea to do that from a component and you should rather do that directly from your service.
Also, if you're looking for a more advanced answer you can take a look here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With