Na estrada

Estou na estrada. Sozinha. Cresci acreditando que lugar de mulher não fosse na estrada. Ao menos não o meu. Não ao volante. É que desde pequena eu via meu pai guiando e minha mãe no banco do…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Implement Debounce in Javascript

Debouncing in JavaScript is a practice used to improve browser performance. There might be some functionality in a web page which requires time-consuming computations. If such a method is invoked frequently, it might greatly affect the performance of the browser, as JavaScript is a single threaded language. Debouncing is a programming practice used to ensure that time-consuming tasks do not fire so often, that it stalls the performance of the web page. In other words, it limits the rate at which a function gets invoked.

When I implement like below, there is error I found with arrow function. With arrow function, this key word is bound to the orignal context where the function is created. So “The context is undefined” is printed for below snippet.

To avoid this problem, we need to replace arrow function with normal function like below, which will give the result “The context is mycontext” as expected. This key word is bound to the context of the function that is called.

Then, if the function has not already been executed, you will be able to stop the execution by calling the clearTimeout() method.

And if debounce is triggered again within the wait time, the setTimeout will be cleared by clearTimeout and a new setTimeout with wait time will be set again. This will prevent from frequent triggering of the function passed in debounce. For instance, below snippet shows how to avoid constantly triggering input event with every letter input when the user is typing a sentence.

Add a comment

Related posts:

Mashrafe Bin Mortaza needs to buck up!

England have scripted a comprehensive victory against Bangladesh and back to back defeats would obviously hamper the confidence of the Tigers. Now, it’s time for Mashrafe Bin Mortaza to lead from the…

The Exploration of Our Inner World

Thank you Alan for sharing Matias De Stefano’s work and your wonderful insights into it. His work of provides an interesting framework to follow our journey of spiritual evolution. From my own…

How To Embed Video In WordPress Tips For Beginners And Experienced Webmasters

Often novice webmasters are faced with the question of how to embed videos in WordPress. There are many tutorial articles on how to add video clips to your blog, but this article includes only the…