Stop Angular2 CountDown

我是研究僧i 提交于 2019-12-06 12:27:05

You didn't unsubscribe to the right variable. Here is how you should do :

this.count = 100;
this.countDown = Observable.timer(0, 1000)
  .subscribe(x => {
      this.count = this.count - 10;
  });

this.sub = Observable.interval(500)
  .subscribe(x => {
     console.log(this.count);
     if (this.count === 0) {
        this.countDown.unsubscribe();
     }
});

Here is a StackBlitz example

I ended up creating a function called stopTimer() that is :

stopTimer(){
        this.countDown = null;
        this.sub.unsubscribe();
}

It will stop the Timer and will stop the CountDown.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!