1.1.2 • Published 5 years ago

linear-debounce v1.1.2

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

LINEAR

Greenkeeper badge Build Status npm version

npm install linear-debounce

Multiple debounce actions sequencer

Linear allows debouncing more than one function at a time, making it simple to respond to one event with multiple functions, or at different times:

window.addEventListener('scroll', linear({
    '0': () => console.log('this will happen immediately'),
    '2500': () => console.log('this will happen after 2.5 seconds'),
    '5000': () => console.log('this will happen after 5 seconds'),
}));

It is also possible to pass multiple functions to fire on the same delay using an array of functions:

window.addEventListener('scroll', linear({
    '2500': [
        () => console.log('this will happen after 2.5 seconds'),
        () => console.log('this will happen after 2.5 seconds'),
        () => console.log('this will happen after 2.5 seconds')
    ]
}));

Sometimes you would want to cancel the already initiated events from being fired, for example, when your app's state changes and you do not expect the debounced actions anymore. To cancel the events, simply call cancel:

 const isTypingDebouncer = linear({
    '0': () => this.setState({userTyping: true}),
    '3000': () => this.setState({userTyping: false})
});

isTypingDebouncer(); // initiates the debounced actions

isTypingDebouncer.cancel(); // clears existing timeouts, in our case, only `0` will fire.

Note: Calling cancel will not prevent future timeouts from being created. It only clears the ones that have already initiated. Calling isTypingDebouncer again, for example, will re-start the count.

1.1.2

5 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago