Debounce, throttle, and rate-limit utilities in action.
import { debounce, throttle } from '@manufosela/debounce-throttle';
const debounced = debounce(onInput, 300);
const throttled = throttle(onScroll, 200);
Type in the input and see how debounce delays the API call until you stop typing.
import { debounce } from '@manufosela/debounce-throttle';
const debouncedSearch = debounce((value) => {
fetch(`/api/search?q=${value}`);
}, 300);
input.addEventListener('input', (e) => {
debouncedSearch(e.target.value);
});
Scroll inside the box and see how throttle limits the handler calls.
import { throttle } from '@manufosela/debounce-throttle';
const onScroll = throttle(() => {
console.log('scroll tick');
}, 100);
scrollArea.addEventListener('scroll', onScroll);
Click the button multiple times - the function only executes once.
import { once } from '@manufosela/debounce-throttle';
const initOnce = once(() => {
initWidget();
});
button.addEventListener('click', initOnce);
Limited to 5 calls per 3 seconds. Click rapidly to see rate limiting in action.
import { rateLimit } from '@manufosela/debounce-throttle';
const limitedCall = rateLimit(() => {
callApi();
}, 5, 3000);
button.addEventListener('click', limitedCall);