UNPKG

5.27 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { SchedulerLike } from '../types';
3/**
4 * Creates an observable that will wait for a specified time period, or exact date, before
5 * emitting the number 0.
6 *
7 * <span class="informal">Used to emit a notification after a delay.</span>
8 *
9 * This observable is useful for creating delays in code, or racing against other values
10 * for ad-hoc timeouts.
11 *
12 * The `delay` is specified by default in milliseconds, however providing a custom scheduler could
13 * create a different behavior.
14 *
15 * ## Examples
16 *
17 * ### Wait 3 seconds and start another observable
18 *
19 * You might want to use `timer` to delay subscription to an
20 * observable by a set amount of time. Here we use a timer with
21 * {@link concatMapTo} or {@link concatMap} in order to wait
22 * a few seconds and start a subscription to a source.
23 *
24 * ```ts
25 * import { timer, of } from 'rxjs';
26 * import { concatMapTo } from 'rxjs/operators';
27 *
28 * // This could be any observable
29 * const source = of(1, 2, 3);
30 *
31 * const result = timer(3000).pipe(
32 * concatMapTo(source)
33 * )
34 * .subscribe(console.log);
35 * ```
36 *
37 * ### Take all of the values until the start of the next minute
38 *
39 * Using a `Date` as the trigger for the first emission, you can
40 * do things like wait until midnight to fire an event, or in this case,
41 * wait until a new minute starts (chosen so the example wouldn't take
42 * too long to run) in order to stop watching a stream. Leveraging
43 * {@link takeUntil}.
44 *
45 * ```ts
46 * import { interval, timer } from 'rxjs';
47 * import { takeUntil } from 'rxjs/operators';
48 *
49 * // Build a Date object that marks the
50 * // next minute.
51 * const currentDate = new Date();
52 * const startOfNextMinute = new Date(
53 * currentDate.getFullYear(),
54 * currentDate.getMonth(),
55 * currentDate.getDate(),
56 * currentDate.getHours(),
57 * currentDate.getMinutes() + 1,
58 * )
59 *
60 * // This could be any observable stream
61 * const source = interval(1000);
62 *
63 * const result = source.pipe(
64 * takeUntil(timer(startOfNextMinute))
65 * );
66 *
67 * result.subscribe(console.log);
68 * ```
69 *
70 * ### Known Limitations
71 *
72 * - The {@link asyncScheduler} uses `setTimeout` which has limitations for how far in the future it can be scheduled.
73 *
74 * - If a `scheduler` is provided that returns a timestamp other than an epoch from `now()`, and
75 * a `Date` object is passed to the `dueTime` argument, the calculation for when the first emission
76 * should occur will be incorrect. In this case, it would be best to do your own calculations
77 * ahead of time, and pass a `number` in as the `dueTime`.
78 *
79 * @param due If a `number`, the amount of time in milliseconds to wait before emitting.
80 * If a `Date`, the exact time at which to emit.
81 * @param scheduler The scheduler to use to schedule the delay. Defaults to {@link asyncScheduler}.
82 */
83export declare function timer(due: number | Date, scheduler?: SchedulerLike): Observable<0>;
84/**
85 * Creates an observable that starts an interval after a specified delay, emitting incrementing numbers -- starting at `0` --
86 * on each interval after words.
87 *
88 * The `delay` and `intervalDuration` are specified by default in milliseconds, however providing a custom scheduler could
89 * create a different behavior.
90 *
91 * ## Example
92 *
93 * ### Start an interval that starts right away
94 *
95 * Since {@link index/interval} waits for the passed delay before starting,
96 * sometimes that's not ideal. You may want to start an interval immediately.
97 * `timer` works well for this. Here we have both side-by-side so you can
98 * see them in comparison.
99 *
100 * Note that this observable will never complete.
101 *
102 * ```ts
103 * import { timer, interval } from 'rxjs';
104 *
105 * timer(0, 1000).subscribe(n => console.log('timer', n));
106 * interval(1000).subscribe(n => console.log('interval', n));
107 * ```
108 *
109 * ### Known Limitations
110 *
111 * - The {@link asyncScheduler} uses `setTimeout` which has limitations for how far in the future it can be scheduled.
112 *
113 * - If a `scheduler` is provided that returns a timestamp other than an epoch from `now()`, and
114 * a `Date` object is passed to the `dueTime` argument, the calculation for when the first emission
115 * should occur will be incorrect. In this case, it would be best to do your own calculations
116 * ahead of time, and pass a `number` in as the `startDue`.
117 * @param startDue If a `number`, is the time to wait before starting the interval.
118 * If a `Date`, is the exact time at which to start the interval.
119 * @param intervalDuration The delay between each value emitted in the interval. Passing a
120 * negative number here will result in immediate completion after the first value is emitted, as though
121 * no `intervalDuration` was passed at all.
122 * @param scheduler The scheduler to use to schedule the delay. Defaults to {@link asyncScheduler}.
123 */
124export declare function timer(startDue: number | Date, intervalDuration: number, scheduler?: SchedulerLike): Observable<number>;
125/**
126 * @deprecated The signature allowing `undefined` to be passed for `intervalDuration` will be removed in v8. Use the `timer(dueTime, scheduler?)` signature instead.
127 */
128export declare function timer(dueTime: number | Date, unused: undefined, scheduler?: SchedulerLike): Observable<0>;
129//# sourceMappingURL=timer.d.ts.map
\No newline at end of file