Advertisement
Viv1r

Untitled

Jun 28th, 2022 (edited)
228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.49 KB | None
  1. const debounce = (fn, delay, immediate) => {
  2.   let timeout;
  3.   if (immediate) {
  4.     let locked = false;
  5.     return function(...args) {
  6.       clearTimeout(timeout);
  7.       if (!locked) {
  8.         fn.apply(this, arguments);
  9.         locked = true;
  10.       }
  11.       timeout = setTimeout(() => {
  12.         locked = false;
  13.       }, delay);
  14.     }
  15.   }
  16.   else return function(...args) {
  17.     clearTimeout(timeout);
  18.     timeout = setTimeout(() => {
  19.       fn.apply(this, arguments);
  20.     }, delay);
  21.   }
  22. }
Advertisement
RAW Paste Data Copied
Advertisement