rxjs takeUntil 不执行finalize
rxjs takeUntil do not execute finalize
我有以下倒计时:
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
打断时执行
您可以使用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
我有以下倒计时:
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
您可以使用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