
Explain Throttling with Examples
20 January, 2023
3
3
0
Contributors
Throttling is a technique in JavaScript that is similar to debouncing, but it is used to limit the rate at which a function can be called. While debouncing ensures that a function is only called once or a certain number of times in a given period, throttling ensures that the function is called at most once within a given period.
Here is an example of a throttle function that will only allow the given function to be called once every 500 milliseconds:
function throttle(fn, limit) {
let lastFn;
let lastRan;
return function() {
const context = this;
const args = arguments;
if (!lastRan) {
fn.apply(context, args);
lastRan = Date.now();
} else {
clearTimeout(lastFn);
lastFn = setTimeout(function() {
if ((Date.now() - lastRan) >= limit) {
fn.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
}
}
You can use the throttle function like this:
const throttledFn = throttle(function() {
console.log('This function is throttled');
}, 500);
// The function will be called once every 500 milliseconds
throttledFn();
throttledFn();
throttledFn();
The main use cases of throttling are to limit the rate of requests sent to a server. For example, if a user is scrolling a webpage, an event is triggered every time the user scroll, to prevent the browser from being overwhelmed by too many requests, you could use a throttle function to limit the rate at which the function that handles the scrolling event is called.
Another example is when you are listening to an event, and you only want to take action after the user has stopped doing it for a certain amount of time. The rate of the events is still fast, but you don't want to take action on every event, just on the ones after the user has stopped doing the action for a certain amount of time.
Throttling and debouncing are similar, but the main difference is that debouncing will only execute the function after a certain period with no events. In contrast, throttling will execute the function at most once in a certain period.
Here are some examples of use cases for throttling in JavaScript:
- Scrolling events: As mentioned before, when a user scrolls a webpage, the
scroll
event is triggered multiple times. If you have a function that needs to be called every time the user scrolls, such as lazy loading images or updating the position of a fixed header, you can throttle it to avoid overwhelming the browser with too many requests. - Resizing events: Similarly, when a user resizes their browser window, the
resize
event is triggered multiple times. Suppose you have a function that needs to be called every time the window is resized, such as adjusting the layout of elements or recalculating positions. In that case, you can throttle it to avoid doing unnecessary calculations and DOM updates. - Keyboard events: When a user types quickly, many
keydown
events are triggered. If you have a function that needs to be called every time a user types, such as validating a form or sending an autocomplete request, you might want to throttle it to avoid sending too many requests to the server. - MouseMove events: When a user moves a mouse, many
mousemove
events are triggered. If you have a function that needs to be called every time a user moves the mouse, such as updating a tooltip or following a cursor, you might want to throttle it to avoid overloading the browser. - Drag and drop events: when the user is holding an object and moves it over a drop zone, there will be multiple events triggering each time the user moves the object. Throttling the function will reduce the number of events triggering but will still allow the action to be executed with a certain rate of frequency.
Throttling is helpful in situations where you have an event that is triggered multiple times in quick succession. Still, you only want a function to be executed at most once within a certain period. By using throttling, you can ensure that the function is called at the appropriate rate and avoid overwhelming the browser or the server with too many requests.