How to stop an interval on an Observable in RxJS

This is possible to stop an interval once it’s going?

You can use the .takeUntil operator to complete an observable when some other observable emits. You’ll of course need to set that other observable up to emit values in a way that is useful to you, but here’s an example that stops an interval after 5 seconds:

Rx.Observable.interval(100)
  .takeUntil(Rx.Observable.timer(5000))
  .subscribe(val => console.log(val));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.min.js"></script>

Leave a Comment