Angular 2 setinterval() keep running on other component

前端 未结 3 1019
醉酒成梦
醉酒成梦 2020-12-23 16:11

I have the following method in one component:

ngOnInit()
        {
            this.battleInit();
            setInterval(() => {
                this.bat         


        
3条回答
  •  醉酒成梦
    2020-12-23 17:00

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

    this.id is an identifier returned by the setInterval that can be used to cancel the operation using clearInterval.

提交回复
热议问题