useEffect(() => { // Cancels any pending timeout and schedules a new call schedule( (a, b) => { doSomething(a, b); }, 500, // Timeout delay in milliseconds a, b, // Arguments passed to the callback );
// Cancels the last scheduled call cancel(); }, []);
Returns an API that delays invoking a callback until after a timeout.
The delayed invocation is automatically cancelled when the component unmounts.
The timeout should be started or stopped only after the component has mounted. Before that, calling either function is a no-op.