相关疑难解决方法(0)

如何取消订阅/停止Observable?

我使用以下代码作为计时器:

export class TimerService {
  private ticks: number = 0;
  private seconds: number = 0;
  private timer;

  constructor(seconds: number) {
    this.seconds = seconds;
    this.timer = Observable.timer(2000, 1000);
    this.timer.subscribe(t => {
      this.ticks = t;
      this.disactivate();
    });
  }

  private disactivate() {
    if (this.ticks === this.seconds) {
      this.timer.dispose();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试停止计时器时:

this.timer.dispose(); // this.timer.unsubscribe();
Run Code Online (Sandbox Code Playgroud)

它对我不起作用

observable rxjs angular

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1

observable ×1

rxjs ×1