简体   繁体   中英

Start RxJava2 Observable.interval() with no initial delay?

I'm using a RxJava2 Observable to repeat an operation every second and later post the results to a TextView in my Android layout. The code I have right now (below) is working great, but I would rather have it execute with no initial delay.

Disposable disposable = Observable.interval(1000, java.util.concurrent.TimeUnit.MILLISECONDS)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(aLong -> myMethod());

I've noticed that others using RxJS have used timer() instead of interval() to achieve 0 delay. Is there any way to do this with RxJava2?

There is a Observable.timer(long delay, TimeUnit unit, Scheduler scheduler) in RxJava 2 . You can try using that.

Update 1:
For an interval that starts with an initial delay and then emits on a regular interval, you can use Observable.interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM