import { Observable } from '../Observable'; import { SchedulerAction, SchedulerLike } from '../types'; import { async } from '../scheduler/async'; import { isNumeric } from '../util/isNumeric'; import { isScheduler } from '../util/isScheduler'; import { Subscriber } from '../Subscriber'; /** * Creates an Observable that starts emitting after an `dueTime` and * emits ever increasing numbers after each `period` of time thereafter. * * <span class="informal">Its like {@link index/interval}, but you can specify when * should the emissions start.</span> * *  * * `timer` returns an Observable that emits an infinite sequence of ascending * integers, with a constant interval of time, `period` of your choosing * between those emissions. The first emission happens after the specified * `dueTime`. The initial delay may be a `Date`. By default, this * operator uses the {@link asyncScheduler} {@link SchedulerLike} to provide a notion of time, but you * may pass any {@link SchedulerLike} to it. If `period` is not specified, the output * Observable emits only one value, `0`. Otherwise, it emits an infinite * sequence. * * ## Examples * ### Emits ascending numbers, one every second (1000ms), starting after 3 seconds * ```ts * import { timer } from 'rxjs'; * * const numbers = timer(3000, 1000); * numbers.subscribe(x => console.log(x)); * ``` * * ### Emits one number after five seconds * ```ts * import { timer } from 'rxjs'; * * const numbers = timer(5000); * numbers.subscribe(x => console.log(x)); * ``` * @see {@link index/interval} * @see {@link delay} * * @param {number|Date} [dueTime] The initial delay time specified as a Date object or as an integer denoting * milliseconds to wait before emitting the first value of 0`. * @param {number|SchedulerLike} [periodOrScheduler] The period of time between emissions of the * subsequent numbers. * @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 `0` after the * `dueTime` and ever increasing numbers after each `period` of time * thereafter. * @static true * @name timer * @owner Observable */ export function timer(dueTime: number | Date = 0, periodOrScheduler?: number | SchedulerLike, scheduler?: SchedulerLike): Observable<number> { let period = -1; if (isNumeric(periodOrScheduler)) { period = Number(periodOrScheduler) < 1 && 1 || Number(periodOrScheduler); } else if (isScheduler(periodOrScheduler)) { scheduler = periodOrScheduler as any; } if (!isScheduler(scheduler)) { scheduler = async; } return new Observable(subscriber => { const due = isNumeric(dueTime) ? (dueTime as number) : (+dueTime - scheduler.now()); return scheduler.schedule(dispatch, due, { index: 0, period, subscriber }); }); } interface TimerState { index: number; period: number; subscriber: Subscriber<number>; } function dispatch(this: SchedulerAction<TimerState>, state: TimerState) { const { index, period, subscriber } = state; subscriber.next(index); if (subscriber.closed) { return; } else if (period === -1) { return subscriber.complete(); } state.index = index + 1; this.schedule(state, period); }
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 |
|