Function debounce

  • Creates a debounced function that delays the given func by a given wait time in milliseconds. If the method is called again before the timeout expires, the previous call will be aborted.

    Type Parameters

    • T extends any[]

      The arguments of the provided function.

    Parameters

    • fn: ((this: DebouncedFunction<T>, ...args: T) => void)

      The function to debounce.

    • wait: number

      The time in milliseconds to delay the function.

    Returns DebouncedFunction<T>

    The debounced function.

    import { debounce } from "@std/async/debounce";

    const log = debounce(
    (event: Deno.FsEvent) =>
    console.log("[%s] %s", event.kind, event.paths[0]),
    200,
    );

    for await (const event of Deno.watchFs("./")) {
    log(event);
    }
    // wait 200ms ...
    // output: Function debounced after 200ms with baz