/bouncer

Bouncer: a Javascript plugin to throttle and debounce functions

Primary LanguageJavaScript

Bouncer

Bouncer: a Javascript plugin to throttle and debounce callbacks easily.

Installation

Install with Bower

bower install Bouncer

Or by cloning this repository.

How to use

The plugin can be used with or without jQuery.

The syntax is like so:

namespace.option(delay,callback,runBefore);
  • namespace: use $ with jQuery, use bouncer without
  • option: throttle or debounce
  • delay: number - use 0 or greater in milliseconds
  • callback: your callback function, eg an ajax call
  • runBefore: for debounce, run your callback before or after the delay

With jQuery

Use the $ namespace

$.throttle(500,callback);
$.debounce(500,callback[,true]);
$("#foo").click($.debounce(500,function() {
  // My ajax callback
});

Plain Javascript

Use the bouncer namespace

bouncer.throttle(500,callback);  
bouncer.debounce(500,callback[,true]);  
document.getElementById('foo')addEventListener('click', bouncer.debounce(500,function(){
// My ajax callback  
}),false);