debounce.js 620 B

123456789101112131415
  1. // Debounce is a higher order function which makes its enclosed function be executed
  2. // only if the function wasn't called again till 'delay' time has passed, this helps reduce impact of heavy working
  3. // functions which might be called frequently
  4. // NOTE : Don't use lambda functions as this doesn't get bound properly in them, use the 'function (args) {}' format
  5. const debounce = (func, delay) => {
  6. let inDebounce
  7. return function () {
  8. const context = this
  9. const args = arguments
  10. clearTimeout(inDebounce)
  11. inDebounce = setTimeout(() => func.apply(context, args), delay)
  12. }
  13. }
  14. export default debounce