86119918ab7274b7acd3a877b1534fbd83d1315669ae0ff9593e0deb3bf12730646ff6e2cda490ae7c029f308e21d15e2fa9ac1c56dc566deaa79918820437 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # throttle-debounce
  2. [![Build Status][ci-img]][ci]
  3. Throttle/debounce your functions.
  4. This module is the same as [jquery-throttle-debounce][jquery-throttle-debounce] ([with some differences](#differences-with-original-module)), but it’s transferred to CommonJS so it can be easily used with tools like Browserify or Webpack.
  5. ## Install
  6. ```sh
  7. npm install throttle-debounce --save
  8. ```
  9. ## Usage
  10. ```js
  11. var throttle = require('throttle-debounce/throttle');
  12. var debounce = require('throttle-debounce/debounce');
  13. throttle(300, function () {
  14. // Throttled function
  15. });
  16. debounce(300, function () {
  17. // Debounced function
  18. });
  19. ```
  20. ## API
  21. ### throttle(delay, noTrailing, callback, debounceMode)
  22. Returns: `Function`
  23. Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.
  24. #### delay
  25. Type: `Number`
  26. A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  27. #### noTrailing
  28. Type: `Boolean`
  29. Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset)
  30. #### callback
  31. Type: `Function`
  32. A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is, to `callback` when the throttled-function is executed.
  33. #### debounceMode
  34. Type: `Boolean`
  35. If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end), schedule `callback` to execute after `delay` ms.
  36. ### debounce(delay, atBegin, callback)
  37. Returns: `Function`
  38. Debounce execution of a function. Debouncing, unlike throttling, guarantees that a function is only executed a single time, either at the very beginning of a series of calls, or at the very end.
  39. #### delay
  40. Type: `Number`
  41. A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  42. #### atBegin
  43. Type: `Boolean`
  44. Optional, defaults to false. If `atBegin` is false or unspecified, callback will only be executed `delay` milliseconds after the last debounced-function call. If `atBegin` is true, callback will be executed only at the first debounced-function call. (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
  45. #### callback
  46. Type: `Function`
  47. A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is, to `callback` when the debounced-function is executed.
  48. ## Differences with original module
  49. * Dependancy on jQuery is removed, so if you rely on GUIDs set by jQuery, plan accordingly
  50. * There is no standalone version available, so don’t rely on `$.throttle` and `$.debounce` to be available
  51. ## Browser support
  52. Tested in IE8+ and all modern browsers.
  53. ## License
  54. **Original module license:** Copyright (c) 2010 "Cowboy" Ben Alman (Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/)
  55. **This module license:** MIT © [Ivan Nikolić](http://ivannikolic.com)
  56. [ci]: https://travis-ci.org/niksy/throttle-debounce
  57. [ci-img]: https://img.shields.io/travis/niksy/throttle-debounce/master.svg
  58. [jquery-throttle-debounce]: https://github.com/cowboy/jquery-throttle-debounce