import { Observable } from '../Observable'; import { async } from '../scheduler/async'; import { SchedulerAction, SchedulerLike } from '../types'; import { isNumeric } from '../util/isNumeric'; import { Subscriber } from '../Subscriber'; /** * Creates an Observable that emits sequential numbers every specified * interval of time, on a specified {@link SchedulerLike}. * * <span class="informal">Emits incremental numbers periodically in time. * </span> * *  * * `interval` returns an Observable that emits an infinite sequence of * ascending integers, with a constant interval of time of your choosing * between those emissions. The first emission is not sent immediately, but * only after the first period has passed. By default, this operator uses the * `async` {@link SchedulerLike} to provide a notion of time, but you may pass any * {@link SchedulerLike} to it. * * ## Example * Emits ascending numbers, one every second (1000ms) up to the number 3 * ```ts * import { interval } from 'rxjs'; * import { take } from 'rxjs/operators'; * * const numbers = interval(1000); * * const takeFourNumbers = numbers.pipe(take(4)); * * takeFourNumbers.subscribe(x => console.log('Next: ', x)); * * // Logs: * // Next: 0 * // Next: 1 * // Next: 2 * // Next: 3 * ``` * * @see {@link timer} * @see {@link delay} * * @param {number} [period=0] The interval size in milliseconds (by default) * or the time unit determined by the scheduler's clock. * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for scheduling * the emission of values, and providing a notion of "time". * @return {Observable} An Observable that emits a sequential number each time * interval. * @static true * @name interval * @owner Observable */ export function interval(period = 0, scheduler: SchedulerLike = async): Observable<number> { if (!isNumeric(period) || period < 0) { period = 0; } if (!scheduler || typeof scheduler.schedule !== 'function') { scheduler = async; } return new Observable<number>(subscriber => { subscriber.add( scheduler.schedule(dispatch, period, { subscriber, counter: 0, period }) ); return subscriber; }); } function dispatch(this: SchedulerAction<IntervalState>, state: IntervalState) { const { subscriber, counter, period } = state; subscriber.next(counter); this.schedule({ subscriber, counter: counter + 1, period }, period); } interface IntervalState { subscriber: Subscriber<number>; counter: number; period: number; }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.ts | File | 5.42 KB | 0644 |
|
SubscribeOnObservable.ts | File | 1.59 KB | 0644 |
|
bindCallback.ts | File | 15.9 KB | 0644 |
|
bindNodeCallback.ts | File | 15.41 KB | 0644 |
|
combineLatest.ts | File | 21.95 KB | 0644 |
|
concat.ts | File | 9.43 KB | 0644 |
|
defer.ts | File | 2.53 KB | 0644 |
|
empty.ts | File | 2.21 KB | 0644 |
|
forkJoin.ts | File | 9.49 KB | 0644 |
|
from.ts | File | 3.06 KB | 0644 |
|
fromArray.ts | File | 423 B | 0644 |
|
fromEvent.ts | File | 10.38 KB | 0644 |
|
fromEventPattern.ts | File | 7.14 KB | 0644 |
|
fromIterable.ts | File | 511 B | 0644 |
|
fromObservable.ts | File | 485 B | 0644 |
|
fromPromise.ts | File | 439 B | 0644 |
|
generate.ts | File | 12.55 KB | 0644 |
|
iif.ts | File | 3.22 KB | 0644 |
|
interval.ts | File | 2.53 KB | 0644 |
|
merge.ts | File | 9.94 KB | 0644 |
|
never.ts | File | 1.16 KB | 0644 |
|
of.ts | File | 4.77 KB | 0644 |
|
onErrorResumeNext.ts | File | 3.83 KB | 0644 |
|
pairs.ts | File | 3.06 KB | 0644 |
|
partition.ts | File | 2.61 KB | 0644 |
|
race.ts | File | 5.03 KB | 0644 |
|
range.ts | File | 2.28 KB | 0644 |
|
throwError.ts | File | 2.2 KB | 0644 |
|
timer.ts | File | 3.32 KB | 0644 |
|
using.ts | File | 3.04 KB | 0644 |
|
zip.ts | File | 12.06 KB | 0644 |
|