delayWhen

签名: delayWhen(selector: Function, sequence: Observable): Observable

延迟发出值,延迟时间由提供函数决定。

delayWhen - 图1

示例

示例 1: 基于 observable 的延迟

( jsBin |
jsFiddle )

  1. import { interval } 'rxjs/observable/interval';
  2. import { timer } 'rxjs/observable/timer';
  3. import { delayWhen } from 'rxjs/operators';
  4. // 每1秒发出值
  5. const message = interval(1000);
  6. // 5秒后发出值
  7. const delayForFiveSeconds = () => timer(5000);
  8. // 5秒后,开始发出 interval 延迟的值
  9. const delayWhenExample = message.pipe(delayWhen(delayForFiveSeconds));
  10. // 延迟5秒后输出值
  11. // 例如, 输出: 5s....1...2...3
  12. const subscribe = delayWhenExample.subscribe(val => console.log(val));

其他资源


:file_folder: 源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/delayWhen.ts