Search code examples
reactive-extensions-js

How to buffer events with Rx extentions for JS


I wan't to get text change events from input but limit them to 3 per second.

Rx support my wish? how can I achieve this ability?


Solution

  • Assuming you already have the Observeable, insert a call to throttle(333) before you consume should do the trick. (333= miliseconds which is 1000/3 which gives you 3 chars per second).

    a very simple sample:

     $(document).ready(function () {
       var mainCanvas = $("#TextBox1");
       var observable = Rx.Observable.FromHtmlEvent(mainCanvas.context, "keypress");
    
       var throttle = observable.throttle(333);
    
       throttle.subscribe(function (next) {
             $('div#test').append(String.fromCharCode( next.charCode)); 
            }
       );
     });