delay

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

Delay emitted values by given time.

delay - 图1

Examples

Example 1: Delay for increasing durations

( jsBin |
jsFiddle )

  1. import { of } 'rxjs/observable/of';
  2. import { merge } from 'rxjs/observable/merge';
  3. import { mapTo, delay } from 'rxjs/operators';
  4. //emit one item
  5. const example = of(null);
  6. //delay output of each by an extra second
  7. const message = merge(
  8. example.pipe(mapTo('Hello')),
  9. example.pipe(mapTo('World!'), delay(1000)),
  10. example.pipe(mapTo('Goodbye'), delay(2000)),
  11. example.pipe(mapTo('World!'), delay(3000))
  12. );
  13. //output: 'Hello'...'World!'...'Goodbye'...'World!'
  14. const subscribe = message.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/delay.ts