1.0.0 • Published 5 years ago

@ngard/tiny-debounce v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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);