JSR

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.

If an AbortSignal is provided via options.signal, aborting the signal clears any pending debounce timeout, equivalent to calling DebouncedFunction.clear.

Examples

Usage

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: [modify] /path/to/file

With AbortSignal

import { debounce } from "@std/async/debounce";
const controller = new AbortController();
const log = debounce(
  (event: Deno.FsEvent) =>
    console.log("[%s] %s", event.kind, event.paths[0]),
  200,
  { signal: controller.signal },
);
for await (const event of Deno.watchFs("./")) {
  log(event);
}
// Abort clears any pending debounce
controller.abort();

Type Parameters

The arguments of the provided function.

Parameters

The function to debounce.

The time in milliseconds to delay the function. Must be a positive integer.

Optional parameters.

Return Type

The debounced function.

Throws

If wait is not a non-negative integer.

Read the original on jsr.io ↗