What is throttleit?
The throttleit package is a utility that allows you to throttle a function, meaning it limits the rate at which a function can fire. This is particularly useful for handling repetitive events that you want to control, such as window resizing, scrolling, or keypresses in a performant way.
What are throttleit's main functionalities?
Throttling function calls
This code sample demonstrates how to use throttleit to throttle a window resize event handler. The onResize function will not be called more than once every 200 milliseconds, no matter how often the resize event fires.
const throttle = require('throttleit');
function onResize(event) {
console.log('Window resized.');
}
window.addEventListener('resize', throttle(onResize, 200));
Other packages similar to throttleit
lodash.throttle
lodash.throttle is a function from the popular Lodash library that provides a similar throttling functionality. It is often used for the same purposes as throttleit but comes as part of a larger utility library, which might be more suitable for projects that already use Lodash for other utilities.
bottleneck
Bottleneck is a package that is more focused on rate-limiting and not just throttling. It can be used to limit the execution of functions to a maximum number of times per interval, which is useful for managing API rate limits or other scenarios where you need a more robust solution than simple throttling.
throttle
Throttle a function
Installation
$ component install component/throttle
$ npm install throttleit
Example
var throttle = require('throttle');
window.onresize = throttle(resize, 200);
function resize(e) {
console.log('height', window.innerHeight);
console.log('width', window.innerWidth);
}
API
throttle(fn, wait)
Creates a function that will call fn
at most once every wait
milliseconds.
Supports leading and trailing invocation.
fn
will receive last context (this
) and last arguments passed to a throttled wrapper before fn
was invoked.
License
MIT