您的位置:首页 > Web前端 > JavaScript

[RxJS] Filtering operators: throttle and throttleTime

2016-05-30 20:58 381 查看
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"

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: