Function useTimeout

  • Returns the protocol that delays invoking a callback until after a timeout.

    The delayed invocation is automatically cancelled on unmount.

    The timeout should be started/stopped after the component is mounted. Before that, it is a no-op.

    Returns [schedule: Schedule, cancel: (() => void)]

    const [schedule, cancel] = useTimeout();

    useEffect(() => {
    // Cancels pending debounce and schedules the new call
    schedule(
    (a, b) => {
    doSomething(a, b);
    },
    500, // Timeout after which the callback is called
    a, b, // Varargs that are passed to the callback
    );

    // Cancels the last debounce call
    cancel();
    }, []);