我用下面的代碼計時器:如何取消訂閱/停止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();
}
}
}
當我嘗試在網上停止定時器:
this.timer.dispose(); // this.timer.unsubscribe();
它不工作對我來說
你想要什麼用這個存檔? –
我需要停止計時器並以秒爲單位修復最後的計時器時間。 – OPV