Skip to content

ealush/linear-debounce

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

Multiple debounce actions sequencer

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published