[RxJS] Filtering operators: throttle and throttleTime

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Filtering operators: throttle and throttleTime相关的知识,希望对你有一定的参考价值。

Debounce is known to be a rate-limiting operator, but it‘s not the only one. This lessons introduces you to throttleTime and throttle, which only drop events (without delaying them) to accomplish rate limiting.

 

 throttleTime(number): first emits, then cause silence
var foo = Rx.Observable.interval(500).take(5);

/*
--0--1--2--3--4|
  debounceTime(1000) // waits for silence, then emits
  throttleTime(1000) // first emits, then causes silence
--0-----2-----4|
*/

var result = foo.throttleTime(1000);

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

 

throttle( () => Observable): 

var foo = Rx.Observable.interval(500).take(5);

/*
--0--1--2--3--4|
  throttle( () => Rx.Observalbe.interval(1000).take(1)) // first emits, then causes silence
--0-----2-----4|
*/

var result = foo.throttle( () => Rx.Observable.interval(1000).take(1));

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

 

Result for both:

/*

"next 0"
"next 2"
"next 4"
"done"

*/

 

以上是关于[RxJS] Filtering operators: throttle and throttleTime的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Filtering operators: throttle and throttleTime

[RxJS] Filtering operators: distinct and distinctUntilChanged

[RxJS] Filtering operator: single, race

RxJS过滤数据流操作符学习 (Filtering Operators)

rxjs/operators:找不到模块

[RxJS] What RxJS operators are