0.0.0 • Published 10 years ago

debounce-on v0.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
10 years ago

debounce-on

debounce a function based on its arguments

Install

$ npm install debounce-on

Use

For instance, you might want to rate-limit a client on a per-endpoint basis:

var debounceOn = require('debounce-on');

var debounceHttp = debounceOn(sendHttpRequest);
debounceHttp('https://google.com');
debounceHttp('https://google.com');
debounceHttp('https://yahoo.com');

// ... wait 100ms

// one request is sent to:
// - google.com
// - yahoo.com

You can pass a hash function that determines what calls the debouncer should coalesce:

var debounceOn = require('debounce-on');

var debounceHttp = debounceOn(sendHttpRequest, function (url) {
  return (url.match(/^https?:\/\/([^\/])\/?/) || {})[1];
});
debounceHttp('https://google.com/a');
debounceHttp('https://google.com/b');
debounceHttp('https://yahoo.com');

// ... wait 100ms

// one request is sent to:
// - google.com/a
// - yahoo.com

API

ver debouncedFn = debounceOn(fn, [timeout], [hash fn]);

fn

The function to debounce.

timeout

The timeout in ms.

hashFn

Function that determines what calls the debouncer should coalesce.

License

MIT