/**
* Returns an Observable that emits a sequential number every specified interval of time.
* <p>
* <img width="640" height="195" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/interval.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code interval} operates by default on the {@code computation} {@link Scheduler}.</dd>
* </dl>
*
* @param interval
* interval size in time units (see below)
* @param unit
* time units to use for the interval size
* @return an Observable that emits a sequential number each time interval
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#interval">RxJava wiki: interval</a>
*/
public final static Observable<Long> interval(long interval, TimeUnit unit) {
return interval(interval, unit, Schedulers.computation());
}
interval操作符,类似于计数器,测试代码如下:
public void test() {
Observable.interval(1, TimeUnit.SECONDS).subscribe(new Subscriber<Long>() {
@Override public void onCompleted() {
Log.e("------ onNext ", "onCompleted");
}
@Override public void onError(Throwable e) {
}
@Override public void onNext(Long aLong) {
Log.e("------ onNext ", "aLong : " + aLong);
}
});
}
日志如下:
04-22 19:40:37.542 11947-11960/com.fernandocejas.android10.sample.presentation E/------ onNext﹕ aLong : 0
04-22 19:40:38.532 11947-11960/com.fernandocejas.android10.sample.presentation E/------ onNext﹕ aLong : 1
04-22 19:40:39.542 11947-11960/com.fernandocejas.android10.sample.presentation E/------ onNext﹕ aLong : 2