72

I want my observable to fire immediately, and again every second. interval will not fire immediately. I found this question which suggested using startWith, which DOES fire immediately, but I then get a duplicate first entry.

Rx.Observable.interval(1000).take(4).startWith(0).subscribe(onNext);

https://plnkr.co/edit/Cl5DQ7znJRDe0VTv0Ux5?p=preview

How can I make interval fire immediately, but not duplicate the first entry?

4 Answers 4

111

Before RxJs 6:

Observable.timer(0, 1000) will start immediately.

RxJs 6+

import {timer} from 'rxjs/observable/timer';
timer(0, 1000).subscribe(() => { ... });
3
49

RxJs 6. Note: With this solution, 0 value will be emitted twice (one time immediately by startWith, and one time by interval stream after the first "tick", so if you care about the value emitted, you could consider startWith(-1) instead of startWith(0)

interval(100).pipe(startWith(0)).subscribe(() => { //your code }); 

or with timer:

import {timer} from 'rxjs/observable/timer';
timer(0, 100).subscribe(() => {

    });
5
  • 2
    for RxJs 6+ interval(100).pipe(startWith(0)).subscribe(() => { //your code });
    – sercanD
    Commented Feb 1, 2019 at 11:21
  • 1
    Regarding RxJs 6: You still need to import startWith (import { startWith } from 'rxjs/operators';)
    – Jette
    Commented Sep 24, 2019 at 5:56
  • If I use startWith, my interval starts at the parameter value of startWith but on the 2nd iteration, goes back to zero IE - .startWith(6) gives a sequence of 6, 0, 1, 2, 3, 4
    – Ste
    Commented Sep 11, 2021 at 20:06
  • yes, if you want 0,1,.. then timer is better. Startwith can be nice if you want to start with null or with -1 or something.
    – Rusty Rob
    Commented Sep 12, 2021 at 6:27
  • Using a similar solution. To start with 0, maybe we can create an immediatelyInterval like this return interval(period).pipe(map(val => val + 1), startWith(0)) Commented Sep 4 at 11:52
9

With RxJava2, there's no issue with duplicated first entry and this code is working fine:

io.reactivex.Observable.interval(1, TimeUnit.SECONDS)
        .startWith(0L)
        .subscribe(aLong -> {
            Log.d(TAG, "test");    // do whatever you want
    });

Note you need to pass Long in startWith, so 0L.

3

RxJava 2

If you want to generate a sequence [0, N] with each value delayed by D seconds, use the following overload:

Observable<Long> interval(long initialDelay, long period, TimeUnit unit)

initialDelay - the initial delay time to wait before emitting the first value of 0L

Observable.interval(0, D, TimeUnit.SECONDS).take(N+1)

You can also try to use startWith(0L) but it will generate sequence like: {0, 0, 1, 2...}

I believe something like that will do the job too:

Observable.range(0, N).delayEach(D, TimeUnit.SECONDS)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.