throttle-debounce
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "throttle-debounce" is a utility npm package aimed to help control the frequency of function execution. It offers throttle and debounce functions which are particularly useful for rate-limiting execution of handlers attached to events like resize and scroll. Being a version of "jquery-throttle-debounce", this module has been transferred to ES Modules and CommonJS format and does not depend on jQuery, making it more versatile and universal.
Using the "throttle-debounce" is straightforward. First, make sure it is installed in your project by running npm install throttle-debounce --save
in your terminal. There are two main functions you can import from 'throttle-debounce': throttle
and debounce
.
Here's an illustration of how to use throttle
:
import { throttle } from 'throttle-debounce';
const throttleFunc = throttle(
1000,
(num) => {
console.log('num:', num);
}
);
throttleFunc(1); // Executes the callback
In this example, 'num: 1' is outputted to the console because callback executes immediately. However, if you call throttleFunc(2)
, it won't execute the callback immediately, but rather waiting for a 1000ms delay.
Here's an illustration of using debounce
:
import { debounce } from 'throttle-debounce';
const debounceFunc = debounce(
1000,
(num) => {
console.log('num:', num);
}
);
debounceFunc(1); // Doesn’t execute the callback immediately.
debounceFunc(2); // The same, won’t execute the callback immediately.
In this example, both calls to debounceFunc()
won't execute the callback immediately. Instead, they wait for a delay of 1000 ms, and call the function with the last received argument.
Documentation for "throttle-debounce" is available on its GitHub page at https://github.com/niksy/throttle-debounce. This documentation provides a comprehensive overview of the API, usage examples, and additional details related to the module. This is an excellent resource for any developers looking to integrate "throttle-debounce" into their projects.