debounce.js 563 B

12345678910111213141516171819202122
  1. // src/utils/debounce.js
  2. export const debounce = (func, timeout, immediate) => {
  3. let timer
  4. return function () {
  5. let context = this
  6. let args = arguments
  7. if (timer) clearTimeout(timer)
  8. if (immediate) {
  9. var callNow = !timer
  10. timer = setTimeout(() => {
  11. timer = null
  12. }, timeout)
  13. if (callNow) func.apply(context, args)
  14. } else {
  15. timer = setTimeout(function () {
  16. func.apply(context, args)
  17. }, timeout)
  18. }
  19. }
  20. }