Angular 2 setinterval() keep running on other component
You need to use clearInterval
method for this within the ngOnDestroy
hook method of your component. For this you need to save the returned value by the setInterval
method.
Here is a sample:
ngOnInit() {
this.battleInit();
this.id = setInterval(() => {
this.battleInit();
}, 5000);
}
ngOnDestroy() {
if (this.id) {
clearInterval(this.id);
}
}
every 40 seconds
polling: any;
ngOnInit() {
this.consulta();
this.pollData();
}
pollData () {
this.polling = setInterval(() => {
this.consulta();
},40*1000)
ngOnDestroy() {
clearInterval(this.polling);
}