Understanding RxJava Timer, Delay, and Interval Operators

In this blog, we are going to learn the RxJava Timer, Delay, and Interval Operators. We will understand when to use Timer operator, when to use Delay operator and when to use Interval operator depending on our use-case.

We will learn all the following operators with examples.

  • Timer
  • Delay
  • Interval

Let's start with the Timer Operator of RxJava

Timer operator is used when we want to do something after a span of time that we specify.

Let's understand Timer operator with an example.

Observable.timer(2, TimeUnit.SECONDS)
    .flatMap {
        return@flatMap Observable.create<String> { emitter ->
            Log.d("TimerExample", "Create")
            emitter.onNext("MindOrks")
            emitter.onComplete()
        }
    }
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe {
        Log.d("TimerExample", it)
    }

Here as we have passed 2 seconds into the Timer operator, it will go into the flatMap operator after 2 seconds. This way we are doing the task after 2 seconds and emitting the value "MindOrks".

Whenever we have the use-case in which we want to do the task after a particular span of time, we can use the Timer Operator.

Now, let's learn the Delay Operator of RxJava

Delay operator shift the emissions from an Observable forward in time by a particular amount.

Let's understand Delay operator with an example.

Observable.create<String> { emitter ->
    Log.d("DelayExample", "Create")
    emitter.onNext("MindOrks")
    emitter.onComplete()
}
.subscribeOn(Schedulers.io())
.delay(2, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
    Log.d("DelayExample", it)
}

Here we are doing some task and then emitting value, but we want to delay the emission of the value to the subscriber, for that we have used the delay operator.

Whenever we have the use-case in which we want to do the task first as usual and delay only the emission for a particular span of time, we can use the Delay Operator.

Now, let's learn the Interval Operator of RxJava

Interval Operator create an Observable that emits a sequence of integers spaced by a given time interval. It is used when we want to do a task again and again after some interval.

Let's understand Interval operator with an example.

val disposable =
    Observable.interval(0, 2, TimeUnit.SECONDS)
        .flatMap {
            return@flatMap Observable.create<String> { emitter ->
                Log.d("IntervalExample", "Create")
                emitter.onNext("MindOrks")
                emitter.onComplete()
            }
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe {
            Log.d("IntervalExample", it)
        }
compositeDisposable.add(disposable)

Here the task will be done again and again after 2 seconds of interval.

One thing to notice: It will keep going on forever.

How to stop that?

There are two ways to stop. The following are the two ways to stop it.

  • Using the compositeDisposable.dispose()
  • Using take(n) operator like below
Observable.interval(0, 2, TimeUnit.SECONDS)
    .take(5)
    .flatMap {
        return@flatMap Observable.create<String> { emitter ->
            Log.d("IntervalExample", "Create")
            emitter.onNext("MindOrks")
            emitter.onComplete()
        }
    }
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe {
        Log.d("IntervalExample", it)
    }

As we have passed 5 as a parameter in the take(5), the task will only be done 5 times with an interval of 2 seconds.

Whenever we have the use-case in which we want to do the task again and again with the particular time interval, we can use the Interval Operator.

One more very important thing to notice is that all the Operators Timer, Delay, and Interval run on Schedulers.computation(). So we do not need to worry about that.

This way we can use RxJava Timer, Delay, and Interval Operators to solve the interesting problem.

Find the complete project here and learn RxJava.

Happy Learning :)

Team MindOrks

Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.