End interval when route changes in Angular 2

后端 未结 3 480
青春惊慌失措
青春惊慌失措 2021-01-05 07:22

I start a timer in an Angular 2 component which is inside a router outlet.

setInterval(() => {
    ...
}, 10000);

When I leave the route

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 07:45

    This should do it:

    routerOnActivate() {
      this.timer = setInterval(()=>{
                    ...
                }, 10000);
    }
    
    routerOnDeactivate() {
      clearInterval(this.timer);
    }
    

提交回复
热议问题