function debounce(callback, delay = 300) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback(...args);
}, delay);
};
}
// Example: avoid running a search request on every keystroke
const search = debounce((query) => {
console.log("Searching for:", query);
}, 500);
search("javascript");
search("javascript debounce");
search("javascript debounce function");
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter