Clearinterval not working inside ngOnDestroy ()

My ngOndestroy calls another route navigation, but does not execute clearInterval inside the method. Where am I doing this wrong? It works in the background, and I in another component.

timer:any; ngOnInit() { this.timer= this.interval(); }; ngOnDestroy(){ clearInterval(this.timer); console.log("Inside Destroy"); } interval(){ setInterval(()=>{ this.getData(); },20000) } getData(){ this.dataservice.getdata() .subscribe(users=>{ this.datas=users; console.log(this.datas); }) } 
+5
source share
2 answers

You forgot to return an instance of the interval.

 interval(){ return setInterval(()=>{ this.getData(); },20000) } 
+4
source

Because you are not returning a timer value.

 // you need to return interval identificator to be able to clear it later. interval(){ return setInterval(()=>{ this.getData(); },20000) } 
+2
source

Source: https://habr.com/ru/post/1274292/


All Articles