zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[rxjs] Throttled Buffering in RxJS (debounce)

in Rxjs debounce
2023-09-14 08:59:20 时间

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);
}