The Interval operator

RxJava source code:

public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit) {
    return interval(initialDelay, period, unit, Schedulers.computation());
}

public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableInterval(Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
}

public static Observable<Long> interval(long period, TimeUnit unit) {
    return interval(period, period, unit, Schedulers.computation());
}

public static Observable<Long> interval(long period, TimeUnit unit, Scheduler scheduler) {
    return interval(period, period, unit, scheduler);
}
Copy the code

There are four different overloaded methods, among which 1, 3 and 4 all call method 2 directly, so you can directly look at method 2. Period and unit are required for all methods. InitialDelay and scheduler can be set by themselves, and default values will be set if they are not passed.

Unit is the unit of interval time, and TimeUnit is an enumeration type. You can directly call the required unit, such as timeUnit.minutes.

Period is the interval of a scheduled task. The unit is unit. The value is specified.

InitialDelay indicates how long the scheduled task will be executed after the first execution. If the scheduled task is not executed, the interval is the same as the later scheduled task by default.

The default scheduler is Schedulers.computation() and can also specify threads.

Below is the code that executes the scheduled task every 100 milliseconds, and the same goes for other methods, passing in parameters as needed.

Observable.interval(100, TimeUnit.MILLISECONDS)
    .subscribe(new Consumer<Long>() {
        @Override
        public void accept(Long aLong) throws Exception {
        
        }
});
Copy the code

IntervalRange operator

Also take a look at the source code of RxJava:

public static Observable<Long> intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit) {
    return intervalRange(start, count, initialDelay, period, unit, Schedulers.computation());
}
public static Observable<Long> intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
    if (count < 0) {
        throw new IllegalArgumentException("count >= 0 required but it was " + count);
    }

    if (count == 0L) {
        return Observable.<Long>empty().delay(initialDelay, unit, scheduler);
    }

    long end = start + (count - 1);
    if (start > 0 && end < 0) {
        throw new IllegalArgumentException("Overflow! start + count is bigger than Long.MAX_VALUE");
    }
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");

    return RxJavaPlugins.onAssembly(new ObservableIntervalRange(start, end, Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
}
Copy the code

There are two different overloaded methods, period, unit, initialDelay, and Scheduler, which are the same as the Interval operator, except for the start and count arguments.

Start: Indicates the start value.

Count: indicates the number to be sent. 0 is not sent. Negative numbers are abnormal.

The following code outputs 10 data starting at 1, delayed by 1 second, and executed every 2 seconds, with output values from 1 to 10.

Observable.intervalRange(1, 10, 1, 2, TimeUnit.SECONDS) .subscribe(new Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { Log.d(TAG, String.valueOf(aLong)); }});Copy the code

IntervalRange is just that, passing in parameters as needed.