我的倒计时如下:
userClick=new Subject()
resetCountdown(){this.userClick.next()}
setCountDown() {
let counter = 5;
let tick = 1000;
this.countDown = timer(0, tick)
.pipe(
take(counter),
map(() => --counter),
takeUntil(this.userClick),
finalize(() => {
if (this.currentQuestionNumber < this.questionsToAsk)
this.showNextQuestion();
else {
this.endQuiz();
}
})
);
}
发生takeUntil(this.userClick)
时,我不希望执行finalize
。是否有可能实现这一目标?我只希望在倒计时达到finalize
且之前没有被0
takeUntil
答案 0 :(得分:1)
您可以使用tap
来检查值是否达到目标并执行副作用。
像
function setCountDown() {
let counter = 5;
let tick = 1000;
this.countDown = timer(0, tick)
.pipe(
take(counter + 1),
map(value => counter - value),
tap(value => {
if (value === 0) {
console.log('OUT OF TIME');
}
}),
takeUntil(userClick)
)
}
查看此example