Currently, I have a sidebar that allows me to click on individual components. These components refresh API calls to get data. However, when I click off the components to another component, the old component keeps on refreshing API data, even though it is not needed as I have clicked off from it.
Here is a test component below. It prints out "test component is still active" when I click on the component. When I click off the component to another component, it still says "test component is still active", this tells me that the component is still running. Is there a way to turn off the component once I have clicked off it? Or is there a better way of getting fresh data?
export class TestComponent implements OnInit {
userActivity;
constructor(
) { }
ngOnInit() {
this.refreshData();
}
refreshData(){
this.userActivity = setTimeout(() => {
// where api data is called out
console.log('test component is still active');
this.refreshData();
}, 5000);
}
}