[rxjs] Throttled Buffering in RxJS (debounce)

Capturing every event can get chatty. Batching events with a throttled buffer in RxJS lets you capture all of those events and use them responsibly without overloading a subscriber downstream.

 

var Observable = Rx.Observable;
var button = document.getElementById('btn');
var clicks = Observable.fromEvent(button,'click');
var source = clicks.scan(0, function(x){
  return  x+1;
})
.buffer(clicks.debounce(1000))
.forEach(function(x){
  sendValues(x);
});

function sendValues(arr) {
  var pre = document.createElement('pre');
  pre.innerHTML = JSON.stringify(arr);
  document.querySelector('#results')
    .appendChild(pre);
}
 

 

posted @ 2015-08-24 21:23  Zhentiw  阅读(387)  评论(0编辑  收藏  举报