我正在用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还是陌生的,不知道如何正确使用它。
任何帮助将不胜感激。
答案 0 :(得分:1)
这是使用rxjs 6的node.js代码段。除非按下p
,否则将发出计时器事件。再次按下,排放继续(ctrl-c
将退出)。
在内部,当暂停器发出false
时,实际上启动了一个新计时器。因此,我们在concat
发射之前为(false
)暂停程序启动第一个计时器。 2个scan
运算符管理链的状态(暂停切换器和计数器)。
import { timer, concat, NEVER, of, fromEvent } from 'rxjs';
import { scan, tap, filter, switchMap } from 'rxjs/operators';
import { emitKeypressEvents } from 'readline';
process.stdin.setRawMode(true);
emitKeypressEvents(process.stdin);
const keypresses$ = fromEvent(process.stdin, 'keypress', (_, key) => key);
const pauser$ = keypresses$.pipe(
tap(key => {
if (key && key.ctrl && key.name == 'c') {
process.exit(0);
}
}),
filter(key => key.name === 'p'),
scan(acc => !acc, false),
);
const starter$ = of(false);
concat(starter$, pauser$)
.pipe(
switchMap(stopped => (stopped ? NEVER : timer(0, 1000))),
scan(acc => acc + 1, 0),
)
.subscribe(console.log);
答案 1 :(得分:1)
我今天(在用Angular实现俄罗斯方块克隆时)也遇到了同样的问题。这就是我最终得到的:
import { Subject, timer } from 'rxjs';
export class Timer {
private timeElapsed = 0;
private timer = null;
private subscription = null;
private readonly step: number;
update = new Subject<number>();
constructor(step: number) {
this.timeElapsed = 0;
this.step = step;
}
start() {
this.timer = timer(this.step, this.step);
this.subscription = this.timer.subscribe(() => {
this.timeElapsed = this.timeElapsed + this.step;
this.update.next(this.timeElapsed);
});
}
pause() {
if (this.timer) {
this.subscription.unsubscribe();
this.timer = null;
} else {
this.start();
}
}
stop() {
if (this.timer) {
this.subscription.unsubscribe();
this.timer = null;
}
}
}
在我的游戏服务中,我像这样使用它:
init() {
this.timer = new Timer(50);
this.timer.start();
this.timer.update.subscribe(timeElapsed => {
if (timeElapsed % 1000 === 0) {
this.step(); // step() runs one game iteration
}
});
}
togglePause() {
this.timer.pause();
}
N.B .:我是Angular / RxJS的新手,所以不确定上面的代码是否不错。但这有效。
答案 2 :(得分:0)
我从未使用过timer()
函数,但是您可以设置这样的标志。
source: Observable<number>;
subscribe: Subscription;
timerPaused: boolean = false;
start() {
this.seconds = 0;
this.minutes = 0;
this.hours = 0;
this.time = 0;
this.source = timer(0, 1000);
this.subscribe = this.source
.subscribe(number => {
if(!this.timerPaused) {
this.toSeconds = this.time % 60;
this.toMinutes = Math.floor(this.time / 60);
this.toHours = Math.floor(this.time / (60 * 60));
this.seconds = (this.toSeconds < 10 ? '0' : '') + this.toSeconds;
this.minutes = (this.toMinutes < 10 ? '0' : '') + Math.floor(number / 60);
this.hours = (this.toHours < 10 ? '0' : '') + Math.floor(number / (60 * 60));
this.time += 1000
}
});
}
onPause() {
this.timerPaused = true;
}
onResume() {
this.timerPaused = false;
}
ngOnDestroy() {
this.subscribe.unsubscribe();
}