Interval操作符的作用


Interval操作符返回一个Observable,它按固定的时间间隔发射一个无限递增的整数序列。

示例说明

RxJava版本:2.2.19

基础实现

  • interval(long period, TimeUnit unit)
  • interval(long period, TimeUnit unit, Scheduler scheduler)

image.png

  1. Observable<Long> interval = Observable.interval(10, TimeUnit.SECONDS);
  2. Observable<Long> interval = Observable.interval(10, TimeUnit.SECONDS, Schedulers.io());

扩展:指定第一个内容发射的间隔

  • interval(long initialDelay, long period, TimeUnit unit)
  • interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler)

image.png

  1. Observable<Long> interval = Observable.interval(5,10, TimeUnit.SECONDS);
  2. Observable<Long> interval = Observable.interval(5,10, TimeUnit.SECONDS, Schedulers.io());

扩展:指定发射内容的初始值和发射数量

Interval和Range操作符结合的实现

  • intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit)
  • intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit, Scheduler scheduler)
    1. Observable<Long> interval = Observable.intervalRange(100,1000,5,10, TimeUnit.SECONDS);
    2. Observable<Long> interval = Observable.intervalRange(100,1000,5,10, TimeUnit.SECONDS,Schedulers.io());

    参考资料

    http://reactivex.io/documentation/operators/interval.html