Skip to content

Instantly share code, notes, and snippets.

@ar5had
Forked from ca0v/debounce.ts
Created October 10, 2022 13:41
Show Gist options
  • Select an option

  • Save ar5had/63d156469b08d559dacb460bdadeb1e7 to your computer and use it in GitHub Desktop.

Select an option

Save ar5had/63d156469b08d559dacb460bdadeb1e7 to your computer and use it in GitHub Desktop.

Revisions

  1. @ca0v ca0v revised this gist Nov 1, 2019. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions debounce.ts
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    function debounce(func: () => void, wait = 50) {
    let h: number;
    return () => {
    // ts 3.6x
    function debounce<T extends Function>(cb: T, wait = 20) {
    let h = 0;
    let callable = (...args: any) => {
    clearTimeout(h);
    h = setTimeout(() => func(), wait);
    h = setTimeout(() => cb(...args), wait);
    };
    }
    return <T>(<any>callable);
    }

    // usage
    let f = debounce((a: string, b: number, c?: number) => console.log(a.length + b + c || 0));
    f("hi", 1, 1);
    f("world", 1);
  2. @ca0v ca0v created this gist Feb 14, 2017.
    7 changes: 7 additions & 0 deletions debounce.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    function debounce(func: () => void, wait = 50) {
    let h: number;
    return () => {
    clearTimeout(h);
    h = setTimeout(() => func(), wait);
    };
    }