You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. just-debounce
  2. =============
  3. just a basic debounce function
  4. # Why?
  5. I searched npm and the first 3 pages of results for "debounce" did not have a
  6. small correctly implemented version of debounce
  7. # Usage
  8. ### arguments
  9. * `fn`: the function to debounce
  10. * `delay`: debounce delay in ms
  11. * `at_start:` if true, the function will be called at the beginning of the
  12. delay rather than the end
  13. * `guarantee`: ensures the time before the next call the `fn` is not greater \
  14. than the delay period.
  15. ```javascript
  16. var db = require('just-debounce')
  17. var debounced = db(function(v) {console.log(v)}, 100)
  18. debounced('hi')
  19. debounced('hi')
  20. // logs 'hi' once after 100ms
  21. ```
  22. ```javascript
  23. var db = require('just-debounce')
  24. var debounced = db(function(v) {console.log(v)}, 100, true)
  25. debounced('hi')
  26. debounced('hi')
  27. // logs 'hi' once right away, but not a second time. calling after 100ms will log again
  28. ```
  29. ```javascript
  30. var db = require('just-debounce')
  31. var debounced = db(function(v) {console.log(v)}, 100, false, true)
  32. debounced('hi')
  33. setTimeout(function() {debounced('hi2')}, 80)
  34. // logs 'hi2' once 100ms after the first call to debounced
  35. ```
  36. # license
  37. MIT