Open In App

Understanding RxJava Timer, Delay, and Interval Operators

Last Updated : 11 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

We will learn about the RxJava Timer, Delay, and Interval Operators in this post. Also, the use case of all the different Timers, Delays, and Interval Operators would be clarified in this article so stay tuned till the end. With examples, we shall study all of the following operations.

  • Timers
  • Delays
  • Interval

Let’s start with the RxJava Timer Operator. The Timer operator is used when we want to do something after a certain amount of time. 

Timer Operator Example

Kotlin




Observable.timer(5, TimeUnit.SECONDS)
    .flatMap {
        return@flatMap Observable.create<String> { shooter ->
            Log.d("gfgArticleExampe", "Shoot")
            shooter.onNext("GeeksforGeeks")
            shooter.onComplete()
            // Action to be done when completed 10 secs
        }
    }
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe {
        Log.d("gfgArticleExampe", it)
}


The above example shows us what we call a timer operator. As we have passed 10 seconds into the Timer operator, it will then move to the other task in this manner, we complete the task in 10 seconds and emit the value “GeeksforGeeks.”

GeekTip #1: When we have a use-case where we wish to complete a job after a certain amount of time, we may use the Timer Operator.

Now, let’s look at the RxJava Delay Operator. The Delay Operator shifts the emissions from an Observable ahead in time by a specified amount. Let’s look at an example of the Delay operator.

Delay Operator Example

Kotlin




Observable.create<String> { emitter ->
    Log.d("DelayGFG", "Shoot")
    emitter.onNext("GeeksforGeeks")
    emitter.onComplete()
}
.subscribeOn(Schedulers.io())
.delay(10, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
    Log.d("DelayGFG", it)
}


We’re doing some work and then emitting a value, but we want to postpone the value’s emission to the subscriber, so we’ve used the delay operator. 

Geek Tip #2: When we have a use-case where we wish to execute the work first and then postpone the emission for a specific amount of time, we may use the Delay Operator.

Let’s take a look at the RxJava Interval Operator, which generates an Observable that emits a sequence of integers separated by a time interval.

Interval Operator Example

Kotlin




val disposable =
    Observable.interval(1, 10, TimeUnit.SECONDS)
        .flatMap {
            return@flatMap Observable.create<String> { shooter ->
                Log.d("GfGInterval", "Shoot")
                shooter.onNext("GeeksforGeeks")
                shooter.onComplete()
            }
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe {
            Log.d("GfGInterval", it)
        }
compositeDisposable.add(disposable)


In this case, the task will be repeated after a 10-second delay. One thing to bear in mind: it will continue indefinitely.

How to get rid of this loop then?

Well, there are two ways to come to a halt:

  • Making use of the compositeDisposable.
  • dispose() can be called using the take(n) operator.

Conclusion

When we have a use-case where we wish to repeat a job with a certain time interval, we may use the Interval Operator. Another thing to bear in mind is that all of the Timer, Delay, and Interval Operators run on the scheduler thread, hence we don’t have to be concerned about it. To address the intriguing problem, we may utilize RxJava Timer, Delay, and Interval Operators.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads