just-throttle

return a throttled function

MIT 18 个版本
安装
npm install just-throttle
yarn add just-throttle
pnpm add just-throttle
bun add just-throttle
README

just-throttle

Part of a library of zero-dependency npm modules that do just do one thing. Guilt-free utilities for every occasion.

🍦 Try it

npm install just-throttle
yarn add just-throttle

Return a throttled function

import throttle from 'just-throttle';

// no matter how many times the function is called, only invoke once within the given interval
// options: 
// `leading`: invoke  before interval
// `trailing`: invoke afer interval

const fn1 = throttle(() => console.log('hello'), 500, {leading: true});
setInterval(fn1, 400);
// logs 'hello' immediately and then every 500ms

const fn2 = throttle(() => console.log('hello'), 500, {trailing: true});
setInterval(fn2, 400);
// logs 'hello' after 500ms and then every 500ms

const fn3 = throttle(() => console.log('hello'), 500, {leading: true, trailing: true});
// forces trailing to false

const fn4 = throttle(() => console.log('hello'), 500, { leading: false });
fn4();
fn4();
fn4();
fn4.cancel();
// function cancelled before 'hello' is logged

const fn5 = throttle(() => console.log("Hello"), 500);
fn5();
fn5();
fn5();
fn5.flush();
// immediately invoke the throttled function
版本列表
4.2.0 2022-12-17
4.1.1 2022-08-08
4.1.0 2022-08-07
4.0.1 2021-11-13
4.0.0 2021-11-09
3.1.1 2021-11-01
3.1.0 2021-10-31
3.0.0 2021-10-22
2.3.1 2021-06-05
2.3.0 2021-05-03
2.2.0 2021-04-30
2.1.1 2021-04-04
2.1.0 2021-04-04
2.0.0 2021-04-03
1.1.0 2018-08-09
1.0.2 2017-05-29
1.0.1 2017-05-29
1.0.0 2017-05-29