@cdellacqua/debounce
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.1 • Public • Published

    Debounce a function with requestAnimationFrame or a custom interval

    NPM Package

    npm install @cdellacqua/debounce

    Documentation

    Highlights

    import {debounce} from '@cdellacqua/debounce';
    
    // It uses requestAnimationFrame by default
    const debounced = debounce(() => {
    	console.log(`Debounced with requestAnimationFrame: ${window.innerWidth}px x ${window.innerHeight}px`);
    });
    
    // It preserves function parameters
    const debounced = debounce((a, b) => {
    	console.log(`Debounced with requestAnimationFrame: ${a} and ${b} were passed`);
    });
    debounced("hello", "test argument");
    
    // The interval can be customized, in this case setTimeout is used
    const debounced = debounce(() => {
    	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
    }, 1000);
    
    // Passing true as the third arguments makes the function call happen at the start
    // of the interval: call then wait instead of wait then call
    const debounced = debounce(() => {
    	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
    }, 1000, true);
    
    // You can cancel or flush (call+cancel) the next scheduled execution
    debounced.flush();
    // ..or...
    debounced.cancel();
    
    
    // It exposes a state property and a state$ store containing the 'idle' or 'debouncing'
    const debounced = debounce(() => {
    	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
    }, 1000, true);
    
    console.log(debounced.state); // 'idle'
    debounced();
    console.log(debounced.state); // 'debouncing'
    // ...after 1000ms
    debounced.state$.subscribe(console.log); // immediately prints 'idle'
    debounced(); // triggers the subscription, printing 'debouncing'
    // ...after 1000ms
    // triggers the subscription, printing 'idle'

    Install

    npm i @cdellacqua/debounce

    DownloadsWeekly Downloads

    58

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    22.9 kB

    Total Files

    7

    Last publish

    Collaborators

    • cdellacqua