How to change value in subscription? using rxJS

后端 未结 1 1489
猫巷女王i
猫巷女王i 2021-01-28 10:02

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

相关标签:
1条回答
  • 2021-01-28 10:59

    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();
      }
    
    0 讨论(0)
提交回复
热议问题