| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { asyncScheduler } from '../scheduler/async'; |
| 3 |
import { SchedulerLike } from '../types'; |
| 4 |
import { timer } from './timer'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Creates an Observable that emits sequential numbers every specified |
| 8 |
* interval of time, on a specified {@link SchedulerLike}. |
| 9 |
* |
| 10 |
* <span class="informal">Emits incremental numbers periodically in time.</span> |
| 11 |
* |
| 12 |
*  |
| 13 |
* |
| 14 |
* `interval` returns an Observable that emits an infinite sequence of |
| 15 |
* ascending integers, with a constant interval of time of your choosing |
| 16 |
* between those emissions. The first emission is not sent immediately, but |
| 17 |
* only after the first period has passed. By default, this operator uses the |
| 18 |
* `async` {@link SchedulerLike} to provide a notion of time, but you may pass any |
| 19 |
* {@link SchedulerLike} to it. |
| 20 |
* |
| 21 |
* ## Example |
| 22 |
* |
| 23 |
* Emits ascending numbers, one every second (1000ms) up to the number 3 |
| 24 |
* |
| 25 |
* ```ts |
| 26 |
* import { interval, take } from 'rxjs'; |
| 27 |
* |
| 28 |
* const numbers = interval(1000); |
| 29 |
* |
| 30 |
* const takeFourNumbers = numbers.pipe(take(4)); |
| 31 |
* |
| 32 |
* takeFourNumbers.subscribe(x => console.log('Next: ', x)); |
| 33 |
* |
| 34 |
* // Logs: |
| 35 |
* // Next: 0 |
| 36 |
* // Next: 1 |
| 37 |
* // Next: 2 |
| 38 |
* // Next: 3 |
| 39 |
* ``` |
| 40 |
* |
| 41 |
* @see {@link timer} |
| 42 |
* @see {@link delay} |
| 43 |
* |
| 44 |
* @param period The interval size in milliseconds (by default) or the time unit determined |
| 45 |
* by the scheduler's clock. |
| 46 |
* @param scheduler The {@link SchedulerLike} to use for scheduling the emission of values, |
| 47 |
* and providing a notion of "time". |
| 48 |
* @return An Observable that emits a sequential number each time interval. |
| 49 |
*/ |
| 50 |
export function interval(period = 0, scheduler: SchedulerLike = asyncScheduler): Observable<number> { |
| 51 |
if (period < 0) { |
| 52 |
// We cannot schedule an interval in the past. |
| 53 |
period = 0; |
| 54 |
} |
| 55 |
|
| 56 |
return timer(period, period, scheduler); |
| 57 |
} |