throttle-now
v1.0.0
Published
A tiny throttle utility function
Maintainers
Readme
throttle-now
A tiny throttle function for JavaScript.
Install
npm install throttle-nowUsage
//es modules import
import throttle from 'throttle-now';
//commonjs import
const throttle = require('throttle-now');
const log = throttle(() => console.log('Throttled!'), 2000);
window.addEventListener('scroll', log);Typescript Usage
const log: () => void = throttle(() => console.log('Throttled!'), 2000);Features
- Limits the execution of a function to at most once per given interval.
- Works in both browser and Node.js environments.
- Lightweight, no dependencies.
Examples
const saveInput = throttle(() => console.log('Saving input...'), 1000);
document.getElementById('input').addEventListener('input', saveInput);
//Even if the user types continuously, the saveInput function runs at most once per second.