Debouncing is a programming technique used to improve the performance and efficiency of certain functions, especially those triggered by events like scrolling, resizing, or typing. The purpose of debounce is to ensure that a function is not executed too frequently, particularly when the event that triggers it occurs rapidly or repeatedly.
When an event is triggered repeatedly in a short period, it can cause the associated event handler function to be executed multiple times in quick succession. This can lead to unnecessary processing and may impact the performance of the application or website.
Debouncing addresses this issue by adding a small delay before invoking the function. If the event is triggered again within that delay, the previous timer is canceled, and a new timer is set. The function is only executed when the delay has passed without any new events occurring.
Simplified Debounce Function in Javascript
function debounce(func, delay) {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
In the above example, the debounce
function takes two parameters: func
, which is the function to be debounced, and delay
, which is the time in milliseconds to wait before executing func
.
...args
gathers all the arguments passed to the function into an array args. This makes it possible to work with a variable number of arguments without explicitly defining them in the function signature.
The returned function is the debounced version of func
, which can be assigned as an event handler. When the event occurs, this debounced function is called. If the event is triggered again within the specified delay
, the previous timer is canceled, and a new timer is set.
What’s setTimeout function:
In JavaScript, setTimeout
is a function that allows you to schedule the execution of a function (or the execution of a code snippet) after a specified delay in milliseconds. The syntax for setTimeout
is as follows:
setTimeout(func, delay, arg1, arg2, ...);
func
: This is the function that you want to execute after the specified delay. It can be an existing function defined elsewhere in your code or an anonymous function. delay
: This is the time delay in milliseconds before the func
is executed. For example, if you set delay
to 1000, the func
will be executed after 1000 milliseconds (1 second). arg1
, arg2
, …: (Optional) These are additional arguments that you can pass to the func
. If you need to pass specific data to the function when it’s executed, you can include these arguments.
setTimeout(() => {
func.apply(this, args);
}, delay);
It uses an arrow function (a concise way of defining functions) as the func
to be executed. The apply
method is then called on func
, which allows you to call the function with a given this
value and arguments provided as an array (args
).
The this
value within the func
will be preserved, and any arguments passed to the setTimeout
function after the delay
value will be used as arguments for the func
.
For example, if you had the following code:
function myFunction(a, b) {
console.log(a + b);
}
const args = [2, 3];
const delay = 1000;
setTimeout(() => {
myFunction.apply(this, args);
}, delay);
After 1000 milliseconds, the function myFunction
will be called with arguments 2
and 3
, and it will log 5
to the console.