Welcome to mirror list, hosted at ThFree Co, Russian Federation.

debounce.ts « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b92fe93a628108926cf98b4ca0a799b5c97e307b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
interface Callable {
  (...args: unknown[]): void;
}

const DEFAULT_DEBOUNCE_DELAY = 300;

export default function debounce<F extends Callable>(fn: F, delayInMs = DEFAULT_DEBOUNCE_DELAY): F {
  let timeout: ReturnType<typeof setTimeout>;

  return (...args: Parameters<F>) => {
    if (timeout) {
      clearTimeout(timeout);
    }

    timeout = setTimeout(() => {
      fn(...args);
    }, delayInMs);
  };
}