How to stop rxjs timer

筅森魡賤 提交于 2020-01-04 06:30:22

问题


I have the following timer:

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();
            }

          })
        );
      }

How can I stop the timer? E.g. when a user clicks on a button...


回答1:


assuming you are using angular, if you use javascript only you can just us fromEvent() to create userClick

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();
            }

          })
        );
      }


来源:https://stackoverflow.com/questions/54810643/how-to-stop-rxjs-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!