Skip to content

Instantly share code, notes, and snippets.

@NickSevens
NickSevens / useDebounceFn.ts
Last active January 24, 2023 09:25
React hook which debounces a function call with a specified delay.
import * as React from "react";
type Timer = ReturnType<typeof setTimeout>;
type SomeFunction = (...args: any[]) => void;
/**
* Debounces a function call with the specified delay.
*
* @param func The original, non debounced function (You can pass any number of args to it).
* @param delay The delay (in ms) for the function to return.
* @returns The debounced function, which will run only if the debounced function has not been called in the last (delay) ms.