3

I am working on my first Angular 2 product. I created this Observable.timer and then subscribed to it. It polls the server for data.

How do I pause and unpause this?

  ngOnInit(): void {
    this.poll = Observable.timer(500,500).subscribe((t: any) => {
      this.request.getRackList(+this.global.zoneId).subscribe(
        incoming => this.import(incoming),
        err => { this.global.err(err) }
      );
    })
    this.canOpenClose = (this.global.mode === 2);
  }
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Jerry Huckins
  • 498
  • 3
  • 7
  • 22

2 Answers2

8

There are multiple approaches to pause an Observable. You could:

  • Unsubscribe / resubscribe when needed
  • .filter() out emissions when paused
  • create a connectable Observable so you can pause / start emissions to all subscribers
Mark van Straten
  • 9,287
  • 3
  • 38
  • 57
0

You can have a look here : RxJS - pause, upon resume give last paused value

Basically, you use the switchMap operator to stop the timer emissions/restart them and a pauser subject to emit a boolean indicating whether you want to pause or restart. If you don't need the latest value, you can replace publishReplay by publish

Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75