summaryrefslogtreecommitdiff
path: root/src/misc/utils.js
blob: 66146c0a34fb54800432fcefd5fbbcb89c105246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export function throttle(fn, timeout) {
  let pending = false;

  return (...args) => {
    if (!pending) {
      pending = true;
      fn(...args);
      setTimeout(() => {
        pending = false;
      }, timeout);
    }
  };
}

export function debounce(fn, timeout) {
  let timeoutId;
  return (...args) => {
    if (timeoutId) clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
      fn(...args);
    }, timeout);
  };
}