Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Latest commit

 

History

History
31 lines (23 loc) · 852 Bytes

File metadata and controls

31 lines (23 loc) · 852 Bytes

Ravikumar Daivajna

import React, { useEffect, useState, useRef } from "react";

export function useTimeout(callback: () => void, delay: number) { const callbackRef = useRef(callback); const timeoutRef = useRef(0);

useEffect(() => { callbackRef.current = callback; }, [callback]);

useEffect(() => { // Clear the existing timer clearTimeout(timeoutRef.current);

// Set a new timer with the updated delay
timeoutRef.current = setTimeout(() => callbackRef.current(), delay);

// Cleanup function to clear the timer on unmount or delay change
return () => clearTimeout(timeoutRef.current);

}, [delay]); }

// if you want to try your code on the right panel // remember to export App() component like below

export function App() { useTimeout(() => { console.log("hello") }, 2000) return

your app
}