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
import { fromEvent, of } from 'rxjs';
import { mergeMap, delay, takeUntil } from 'rxjs/operators';
const mousedown$ = fromEvent(document, 'mousedown');
const mouseup$ = fromEvent(document, 'mouseup');
mousedown$
.pipe(mergeMap(event => of(event).pipe(delay(700), takeUntil(mouseup$))))
.subscribe(event => console.log('Long Press!', event));
Example 2: Delay for increasing durations
// RxJS v6+
import { of, merge } from 'rxjs';
import { mapTo, delay } from 'rxjs/operators';
//emit one item
const example = of(null);
//delay output of each by an extra second
const message = merge(
example.pipe(mapTo('Hello')),
example.pipe(mapTo('World!'), delay(1000)),
example.pipe(mapTo('Goodbye'), delay(2000)),
example.pipe(mapTo('World!'), delay(3000))
);
//output: 'Hello'...'World!'...'Goodbye'...'World!'
const subscribe = message.subscribe(val => console.log(val));

Additional Resources

Last modified 1yr ago
Copy link
On this page
Delay emitted values by given time.
Examples
Related Recipes
Additional Resources