How to change value in subscription? using rxJS

最后都变了- 提交于 2021-02-05 11:58:27

问题


im creating a timer, and need your help im just learning angular & rxJS and i have some question about this

Im creating a timer which have start,stop,pause,reset

and btn reset must 'pause' my timer to 300ms

how to do it? :D

my start timer fnc

startTimer() {
    this.intervalStream$ = interval(1000);
    this.sub = this.intervalStream$
        .subscribe((value) => {
          this.subscribeTimer = this.convertSeconds(this.timeLeft - value);
          this.currentTime = this.timeLeft - value;
        });
  }

and after this start i have

pause() {}

can't understand how to do it :D


回答1:


I create an timer with stop resume and pause https://stackblitz.com/edit/angular-gq9zvk.

Your variables

isstop = new Subject();
  ispause = new Subject();
  private time = 0;
  isRunning = true;
  timer: Observable<number>;
  timerObserver: PartialObserver<number>;

This set timer put in in onInit or connect to click start function

this.timer = interval(1000)
      .pipe(
        takeUntil(this.ispause),
        takeUntil(this.isstop)
      );

    this.timerObserver = {
      next: (_: number) => {  
          this.time += 1;        
      }
    };


    this.timer.subscribe(this.timerObserver);

and these are stop resume and pause functions

goOn() {
    this.isRunning = true;
    this.timer.subscribe(this.timerObserver);
  }
  pauseClick() {
    this.ispause.next();
    this.isRunning = false;

  }

  stopClick() {
    this.time = 0;
    this.isRunning = false;
    this.isstop.next();
  }


来源:https://stackoverflow.com/questions/61217441/how-to-change-value-in-subscription-using-rxjs

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