【发布时间】:2019-03-06 23:13:19
【问题描述】:
我正在用 angular/rxjs6 构建一个简单的秒表,我可以启动计时器,但无法暂停/恢复它。
source: Observable<number>;
subscribe: Subscription;
start() {
this.source = timer(0, 1000);
this.subscribe = this.source
.subscribe(number => {
this.toSeconds = number % 60;
this.toMinutes = Math.floor(number / 60);
this.toHours = Math.floor(number / (60 * 60));
this.seconds = (this.toSeconds < 10 ? '0' : '') + this.toSeconds;
this.minutes = (this.toMinutes < 10 ? '0' : '') + this.toMinutes;
this.hours = (this.toHours < 10 ? '0' : '') + this.toHours;
});
}
pause() {
this.subscribe.unsubscribe(); // not working
}
经过大量搜索,我发现我应该使用switchMap 运算符来完成此操作,但我是 rxjs 新手,不知道如何正确操作。
任何帮助将不胜感激。
【问题讨论】: