throttleTime

签名: throttleTime(duration: number, scheduler: Scheduler): Observable

当指定的持续时间经过后发出最新值。

throttleTime - 图1

示例

示例 1: 每5秒接收最新值

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { throttleTime } 'rxjs/operators';
  3. // 每1秒发出值
  4. const source = interval(1000);
  5. /*
  6. 节流5秒
  7. 节流结束前发出的最后一个值将从源 observable 中发出
  8. */
  9. const example = source.pipe(throttleTime(5000));
  10. // 输出: 0...6...12
  11. const subscribe = example.subscribe(val => console.log(val));
示例 2: 对合并的 observable 节流

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { merge } from 'rxjs/observable/merge';
  3. import { throttleTime, ignoreElements } from 'rxjs/operators';
  4. const source = merge(
  5. // 每0.75秒发出值
  6. interval(750),
  7. // 每1秒发出值
  8. interval(1000)
  9. );
  10. // 在发出值的中间进行节流
  11. const example = source.pipe(throttleTime(1200));
  12. // 输出: 0...1...4...4...8...7
  13. const subscribe = example.subscribe(val => console.log(val));

其他资源


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