Nie Selam
Nie Selam

Reputation: 1451

call an interval() immediately without distoring its intervals

I have an Observable.interval(600000) which runs ever 10 minutes. A component is subscribed to it and it works well. But what I want is, is there away to have it run automatically when the app loads without affecting its interval time of 600000miliseconds?

Note: code is heavily shortened to make a point.

@Injectable()
export class CalculateService {
  result: Subject<any> = new Subject<any>();

  constructor(

  ) {


    Observable.interval(600000)
      .subscribe((data) => {
         this.result.next(true);


      });

  }
}

Upvotes: 1

Views: 61

Answers (2)

Paul A. Trzyna
Paul A. Trzyna

Reputation: 300

You can use Observable.timer(0, 600_000).subscribe etc. This will start right away and emit a value every ten minutes like you wanted.

Upvotes: 1

molamk
molamk

Reputation: 4116

Yes, you can use the startWith operator, and it will emit the first value to your observable.

So if you want it to start right away, give it a value of 0, like startWith(0). In your example it looks like this

Observable.interval(600000)
      .subscribe((data) => {
         this.result.next(true);
}).startWith(0);

Upvotes: 1

Related Questions