gpt4 book ai didi

angular - rxjs takeUntil 不执行finalize

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:58 25 4
gpt4 key购买 nike

我有以下倒计时:

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。有没有可能实现这一目标?我只希望在倒计时达到 0 并且之前未被 takeUntil

打断时执行 finalize

最佳答案

您可以使用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

关于angular - rxjs takeUntil 不执行finalize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com