码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Filtering operators: throttle and throttleTime

时间:2016-05-30 21:42:16      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5543888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!