【发布时间】:2020-09-03 10:20:17
【问题描述】:
我正在开发一个 Angular 9 测验应用程序,我正在使用 RxJS 作为倒数计时器(在容器\记分板\time\time.component.ts 中)并且计时器似乎没有显示。 stopTimer() 函数应该在计时器停止的秒数上停止计时器。选择正确答案后计时器应停止,并且计时器应在问题之间重置。每个问题所用的时间应保存到 elapsedTimes 数组中。请在 Stackblitz 上的 TimeComponent 中查看我的计时器代码:https://stackblitz.com/edit/angular-9-quiz-app。谢谢。
下面的代码是我第一次使用 RxJS 构建倒计时时钟。我最近的代码在 Stackblitz 上。
countdownClock() {
this.timer = interval(1000)
.pipe(
takeUntil(this.isPause),
takeUntil(this.isStop)
);
this.timerObserver = {
next: (_: number) => {
this.timePerQuestion -= 1;
...
}
};
this.timer.subscribe(this.timerObserver);
}
goOn() {
this.timer.subscribe(this.timerObserver);
}
pauseTimer() {
this.isPause.next();
// setTimeout(() => this.goOn(), 1000)
}
stopTimer() {
this.timePerQuestion = 0;
this.isStop.next();
}
我在 TimerService 中使用了 stopTimer() 和 pauseTimer(),因此我可以从不同的组件调用它们。
【问题讨论】:
-
在问题中输入minimal reproducible example。
-
你能用文字写下你的要求吗?
-
是的,我希望倒计时时钟从 20 秒减少到 0 秒。选择正确答案后,时钟应在剩余秒数处停止,每个问题的已用时间应保存在 elapsedTimes 数组中,以便以后计算总完成时间。对于以下每个问题,时钟应重置为 20 秒。用户对每个问题的回答和经过的时间应存储在其自己的数组中(使用 Result 接口)并传递给 ResultsComponent。
-
倒计时时钟无法正常工作。有人可以帮忙吗?
-
通过分叉一个新的堆栈闪电战来修复您的堆栈闪电战。在下面找到我的答案。