JavaScript Restart the timer on an rxjs interval restart timer rxjs

I created a class which sets up a pausable rxjs observable on an interval:

export class RepeatingServiceCall<T> {
  private paused = false;
  private observable: Observable<T>;
  constructor(serviceCall: () => Observable<T>, delay: number) {
    this.observable = interval(delay).pipe(flatMap(() => (!this.paused ? serviceCall() : NEVER)));
  }
  setPaused(paused: boolean) {
    this.paused = paused;
  }
  getObservable() {
    return observable;
  }
}

This seems to work fine but the problem I am trying to solve is that I want the timer to reset when unpaused. So let's say that the interval time is 10 seconds and 5 seconds after the last time the interval emitted, setPaused(false) is called. In that scenario, I want it to emit immediately and then restart the timer. Would something like that be an easy thing to add?

Answer:1

You can achieve the behavior you are describing with the following snippet:

const delay = 1000;

const playing = new BehaviorSubject(false);

const observable = playing.pipe(
  switchMap(e => !!e ? interval(delay).pipe(startWith('start')) : never())
);

observable.subscribe(e => console.log(e));

// play:
playing.next(true);

// pause:
playing.next(false);
  • When the playing Observable emits true, the switchMap operator will return a new interval Observable.
  • Use the startWith operator to emit an event immediately when unpausing.
  • If you wish to have the interval start automatically when subscribing to the observable, then simply initialize the BehaviorSubject with true.

StackBlitz Example

Answer:2

You can abandon the old timer on start and start a new one on start.

const { interval, Subject, fromEvent } = rxjs;
const { takeUntil } = rxjs.operators;

let timer$;

const pause = new Subject();

const obs$ = new Subject();

obs$.subscribe(_ => { console.log('Timer fired') });

function start() {
  timer$ = interval(1000);
  timer$.pipe(takeUntil(pause)).subscribe(_ => { obs$.next(); });
}

function stop() {
  pause.next();
  timer$ = undefined;
}

fromEvent(document.getElementById('toggle'), 'click').subscribe(() => {
  if (timer$) {
    stop();
  } else {
    start();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script>
<button id="toggle">Start/Stop</button>
Answer:3

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

  1. scrapy javascript pagination
  2. scrapy javascript support
  3. scrapy javascript click
  4. scrapy javascript onclick
  5. scrapy javascript site
  6. scrapy javascript generated content
  7. scrapy javascript rendered pages
  8. scrapy javascript link
  9. scrapy javascript next page
  10. scrapy javascript python
  11. scrapy javascript example
  12. scrapy javascript selenium
  13. scrapy javascript website
  14. scrapy javascript variable
  15. scrapy javascript button
  16. scrapy javascript form
  17. scrapy javascript table
  18. scrapy javascript postback
  19. scrapy javascript ajax
  20. scrapy javascript text

I was working on a small JavaScript project to make a tile based game (not homework, don't worry!), however I ran into a problem with my 'tile' class. Each 'tile' has an X and a Y property, for its X ...

I was working on a small JavaScript project to make a tile based game (not homework, don't worry!), however I ran into a problem with my 'tile' class. Each 'tile' has an X and a Y property, for its X ...

I'm using Ramda as my functional programming helper library to build a React app. I'm trying to build myself whereAny. Ramda exposes where which checks if every prop it is given satisfies the ...

I'm using Ramda as my functional programming helper library to build a React app. I'm trying to build myself whereAny. Ramda exposes where which checks if every prop it is given satisfies the ...

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...