Skip to content

A minimal-weight utility equivalent to lodash.debounce

License

Notifications You must be signed in to change notification settings

NickGard/tiny-debounce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tiny-debounce

source bundle size build status license

A minimal utility similar to lodash.debounce. For when every byte counts!


lodash.debounce: bundle size
tiny-debounce: bundle size


Install

npm install @ngard/tiny-debounce

Syntax

debounce(/* function, waitTime [, { leading, trailing, maxWait} ] */);

Parameters

function - [Function] The function to be throttled. waitTime - [Number=0] The amount of time, in milliseconds, to wait between the last invocation of the function and firing the passed function. The passed function will be invoked with the arguments from the most recent invocation of the debounced function. options.leading - [Boolean=false] If true, the passed function will be fired on the leading edge of the debounce time period. options.trailing - [Boolean=true] If true, the passed function will be fired on the trailing edge of the debounce time period. options.maxWait - [Number] The maximum amount of time, in milliseconds, to allow the passed function to be debounced before invoking.

Returns

A debounced function with the methods flush and cancel. debouncedFn.flush() will immediately fire any debounced invocation and stop the debouncing timer. debouncedFn.cancel() will cancel any debounced invocation and stop the debouncing timer.

Note

If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the debounced function is invoked more than once during the wait timeout.

If wait is 0 and leading is false, func invocation is deferred until to the next tick, similar to setTimeout with a timeout of 0.

Example

import { debounce } from '@ngard/tiny-debounce';

const debouncedAnimation = debounce(animateSomething, 16)
window.addListener('scroll', debouncedAnimation);
window.addListener('resize', debouncedAnimation.cancel);

About

A minimal-weight utility equivalent to lodash.debounce

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published