DEV Community

Cover image for JavaScript Debounce, Easiest explanation !(with Code)

JavaScript Debounce, Easiest explanation !(with Code)

jeetvora331 on July 10, 2023

Debouncing is a programming technique that helps to improve the performance of web applications by limiting the frequency of function calls. In thi...
Collapse
 
esponges profile image
Fernando González Tostado

I liked the other use cases. I usually only think in search inputs, but submit buttons should probably also be debounced.

Collapse
 
jeetvora331 profile image
jeetvora331

Thanks !

Collapse
 
ibrahimbagalwa profile image
Ibrahim Bagalwa

Nice one

Collapse
 
fredabod profile image
FredAbod

Thanks this is very usefull

Collapse
 
jeetvora331 profile image
jeetvora331

Thanks for your feedback!

Collapse
 
etharrra profile image
Thar Htoo

Thanks!

Collapse
 
jeetvora331 profile image
jeetvora331

Welcome!

Collapse
 
anuradha9712 profile image
Anuradha Aggarwal

Nice article!!

Collapse
 
majik11 profile image
MJ

does (...args) passed into the return function in debounce() represent the args in debounce(searchData, 3000) when called in debouncedSearchData?

Collapse
 
jeetvora331 profile image
jeetvora331

that represent the arguments passed in the main function (in this case it is searchData()). To keep the things simple, I have not used any arguments in the function searchData().

Collapse
 
gallih_armada_02b2de67e02 profile image
Gallih Armada

Thanks very insightfull bro

Collapse
 
jeetvora331 profile image
jeetvora331

Thanks for your feedback!

Collapse
 
rmaurodev profile image
Ricardo

Love the explanation. Great article.

Collapse
 
jeetvora331 profile image
jeetvora331

Thanks @ricardo

Collapse
 
j471n profile image
Jatin Sharma

I use the following approach, it's also the same:


/**
 * Debounces a function by delaying its execution until a certain amount of time has passed
 * since the last time it was invoked. Only the last invocation within the delay period will be executed.
 *
 * @param {Function} func - The function to be debounced.
 * @param {number} delay - The delay in milliseconds before the function is executed.
 * @returns {Function} - A debounced function.
 */
export function debounce(func, delay) {
  let timeoutId;

  return function (...args) {
    clearTimeout(timeoutId);

    timeoutId = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jeetvora331 profile image
jeetvora331

Nice one !

Collapse
 
paulo_reis_rosa profile image
Paulo Rosa

Really easy and simple explanation!

Collapse
 
aryanjain28 profile image
Aryan Jain

This doesn't work for me for some reason.
Basicallt it repeatedly calls the "myFunc" function whenever I enter a key but with a delay of 3000 milliseconds.

Any idea? TIA!

Image description

Image description

Image description

Collapse
 
raffizulvian profile image
Raffi Zulvian Muzhaffar • Edited

Hi, I've had a similar problem before. You could use a useRef when you initialize debounced to prevent the function run on every keystroke.

const debounced = useRef(debounce(myFunc, 3000)).current;
Enter fullscreen mode Exit fullscreen mode

This problem happens because every time you type, the component rerenders and creates a new instance of debounced. That's why it seems clearTimeout(timer) was not working as expected.

Hope this helps!

Collapse
 
jeetvora331 profile image
jeetvora331

Thanks Raffi, I learned something new today !

Thread Thread
 
ryanzayne profile image
Ryan Zayne • Edited

You could also achieve the one time initialisation like this:

const [debounced]= useState(()=>debounce(myFunc, 3000))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jeetvora331 profile image
jeetvora331 • Edited

checkout this, it might help you

geeksforgeeks.org/implement-search...

Collapse
 
leandro_nnz profile image
Leandro Nuñez

Thanks!

Collapse
 
williamrjribeiro profile image
William R. J. Ribeiro

Nice explanation. Here's my favorive demo comparing debouncing and throttling: web.archive.org/web/20180324022838...

Collapse
 
kevwasonga profile image
Kevin Wasonga

nice explanation with examples

Collapse
 
mitchiemt11 profile image
Mitchell Mutandah

Nice breakdown. I can't wait to apply this! 😎

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
ahmadaliglitch profile image
AhmadALi-glitch • Edited

Thanks !

Collapse
 
yujeongkim profile image
yujeongKim

Thanks!