How to call function every 2 mins in angular2?

Victor Carvalho picture Victor Carvalho · Mar 21, 2016 · Viewed 47.2k times · Source

How do I call a save function every two minutes in Angular2?

Answer

Günter Zöchbauer picture Günter Zöchbauer · Mar 21, 2016

rxjs 6

  import { interval } from 'rxjs';

  interval(2000 * 60).subscribe(x => {
    doSomething();
  });

rxjs 5

You can either use

import {Observable} from 'rxjs'; // Angular 6 
// import {Observable} from 'rxjs/Rx'; // Angular 5

  Observable.interval(2000 * 60).subscribe(x => {
    doSomething();
  });

or just setInterval()

Hint:

Angular >= 6.0.0 uses RxJS 6.0.0 Angular Changelog 6.0.0

RxJS v5.x to v6 Update Guide