且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 2 setinterval()继续在其他组件上运行

更新时间:2023-09-29 22:46:04

您需要在组件的ngOnDestroy挂钩方法中使用clearInterval方法.为此,您需要通过setInterval方法保存返回的值.

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.

以下是示例:

ngOnInit() {
  this.battleInit();
  this.id = setInterval(() => {
    this.battleInit(); 
  }, 5000);
}

ngOnDestroy() {
  if (this.id) {
    clearInterval(this.id);
  }
}