我有以下计时器:
setCountDown() {
let counter = 5;
let tick = 1000;
this.countDown = timer(0, tick)
.pipe(
take(counter),
map(() => --counter),
finalize(() => {
if (this.currentQuestionNumber < this.questionsToAsk)
this.showNextQuestion();
else {
this.endQuiz();
}
})
);
}
如何停止计时器?例如。当用户点击按钮时...
最佳答案
假设你使用的是angular,如果你只使用javascript,你可以只使用fromEvent()
来创建userClick
userClick=new Subject()
click(){userClick.next()}
setCountDown() {
let counter = 5;
let tick = 1000;
this.countDown = timer(0, tick)
.pipe(
take(counter),
map(() => --counter),
takeUntil(userClick),
finalize(() => {
if (this.currentQuestionNumber < this.questionsToAsk)
this.showNextQuestion();
else {
this.endQuiz();
}
})
);
}
关于typescript - 如何停止 rxjs 定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54810643/