This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useDebounce<Func>(func: Func, delay) { | |
const [timer, setTimer] = useState(); //Create timer state | |
const debouncedFunction = ((...args) => { | |
const newTimer = setTimeout(() => { | |
func(...args); | |
}, delay); | |
clearTimeout(timer); //Cancel previous timers | |
setTimer(newTimer); //Save latest timer | |
}) as Func; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
function useDebounce<Func>(func: Func, delay) { | |
const [timer, setTimer] = useState(); //Create timer state | |
const debouncedFunction: Func = (...args) => { | |
const newTimer = setTimeout(() => { | |
func(...args); | |
}, delay); | |
clearTimeout(timer); //Cancel previous timers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
function useDebounce(func, delay) { | |
const [timer, setTimer] = useState(); //Create timer state | |
const debouncedFunction = (...args) => { | |
const newTimer = setTimeout(() => { | |
func(...args); | |
}, delay); | |
clearTimeout(timer); //Cancel previous timers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function helloWorld() { | |
console.log("Hello World!"); | |
} | |
const debouncedHelloWorld = useDebounce(helloWorld, 1000); | |
debouncedHelloWorld(); | |
setTimeout(() => { | |
debouncedHelloWorld(); | |
}, 500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debouncedHelloWorld = useDebounce(helloWorld, 1000); | |
debouncedHelloWorld(); | |
setTimeout(() => { | |
debouncedHelloWorld(); | |
}, 500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-nocheck (We will handle the typescript later) | |
function useDebounce(func, delay) { | |
const debouncedFunction = (...args) => { | |
setTimeout(() => { | |
func(...args) | |
}, delay); | |
}; | |
return debouncedFunction; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-nocheck (We will handle the typescript later) | |
function useDebounce(func, delay) { | |
const debouncedFunction = (...args) => { | |
setTimeout(() => { | |
console.log("This runs after (delay) ms"); | |
}, delay); | |
}; | |
return debouncedFunction; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-nocheck (We will handle the typescript later) | |
function useDebounce(func) { | |
const debouncedFunction = (...args) => {} | |
return debouncedFunction | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-nocheck (We will handle the typescript later) | |
function useDebounce(func) { | |
return func | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useEffect } from "react"; | |
type Timer = ReturnType<typeof setTimeout>; | |
type SomeFunction = (...args: any[]) => void; | |
/** | |
* | |
* @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 | |
*/ |