我有以下计时器:
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();
}
})
);
}
如何停止计时器?例如。当用户点击按钮时...
答案 0 :(得分:1)
假设您使用的是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();
}
})
);
}