Learn RxJS
Search…
delay

signature: delay(delay: number | Date, scheduler: Scheduler): Observable

Delay emitted values by given time.

Examples

Example 1: Delay to recognize long press
1
import { fromEvent, of } from 'rxjs';
2
import { mergeMap, delay, takeUntil } from 'rxjs/operators';
3
4
const mousedown$ = fromEvent(document, 'mousedown');
5
const mouseup$ = fromEvent(document, 'mouseup');
6
7
mousedown$
8
.pipe(mergeMap(event => of(event).pipe(delay(700), takeUntil(mouseup$))))
9
.subscribe(event => console.log('Long Press!', event));
Copied!
Example 2: Delay for increasing durations
1
// RxJS v6+
2
import { of, merge } from 'rxjs';
3
import { mapTo, delay } from 'rxjs/operators';
4
5
//emit one item
6
const example = of(null);
7
//delay output of each by an extra second
8
const message = merge(
9
example.pipe(mapTo('Hello')),
10
example.pipe(mapTo('World!'), delay(1000)),
11
example.pipe(mapTo('Goodbye'), delay(2000)),
12
example.pipe(mapTo('World!'), delay(3000))
13
);
14
//output: 'Hello'...'World!'...'Goodbye'...'World!'
15
const subscribe = message.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 6mo ago