I have the following method in one component:
ngOnInit()
{
this.battleInit();
setInterval(() => {
this.battleInit();
}, 5000);
}
Now, I need to run this interval only if the user is in this specific component, that means that when the user navigate away from this component the interval will stop.
Currently, this.battleInit()
is executed every 5 seconds, even after the user navigates away from this page.
Short question: How do I stop setInterval()
when user navigate away (by routing) to another component?