Skip to content

Instantly share code, notes, and snippets.

@PetukhovArt
Created January 2, 2025 10:21
Show Gist options
  • Save PetukhovArt/dc9a689e32e6ce9ea5ba824fa9fc6282 to your computer and use it in GitHub Desktop.
Save PetukhovArt/dc9a689e32e6ce9ea5ba824fa9fc6282 to your computer and use it in GitHub Desktop.
// Нужно написать свой собственный метод debounce
function debounce(fn, timeout) {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
if (typeof fn === "function") {
fn(...args);
}
}, timeout);
};
}
const fetch = (args) => console.log("fetch", args);
const debounceFetch = debounce(fetch, 500);
debounceFetch(1);
debounceFetch(2);
debounceFetch(3);
debounceFetch(4);
debounceFetch(5);
debounceFetch(6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment