throttle
函数签名: throttle(durationSelector: function(value): Observable | Promise): Observable
以某个时间间隔为阈值,在 durationSelector
完成前将抑制新值的发出\
示例
示例 1: 节流2秒,时间由第2个 observable 决定
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { interval } from 'rxjs';
import { throttle } from 'rxjs/operators';
// 每1秒发出值
const source = interval(1000);
// 节流2秒后才发出最新值
const example = source.pipe(throttle(val => interval(2000)));
// 输出: 0...3...6...9
const subscribe = example.subscribe(val => console.log(val));
示例 2: 使用 promise 进行节流
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { interval } from 'rxjs';
import { throttle, map } from 'rxjs/operators';
// 每1秒发出值
const source = interval(1000);
// 基于 source 自增地增加解析的时间
const promise = val =>
new Promise(resolve =>
setTimeout(() => resolve(`Resolved: ${val}`), val * 100)
);
// 当 promise 解析时发出 source 的项
const example = source.pipe(
throttle(promise),
map(val => `Throttled off Promise: ${val}`)
);
const subscribe = example.subscribe(val => console.log(val));
其他资源
- throttle - 官方文档
- 过滤操作符: throttle 和 throttleTime - André Staltz
源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttle.ts