关于打字稿:如何停止rxjs计时器

How to stop rxjs timer

我有以下计时器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
            }

          })
        );
      }

如何停止计时器? 例如。 当用户点击按钮时...


假设您使用的是angular,如果仅使用javascript,则可以使用fromEvent()创建userClick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
            }

          })
        );
      }