Code Monkey home page Code Monkey logo

async-debounce's Introduction

async-debounce

Debounce asynchronous functions.

Debouncing means that given function is only run after no calls to it have happened for x milliseconds. With functions that do asynchronous work, i.e. not finish in the same tick, you also want no calls to happen while the function is currently running - limit the concurrency to one - and rerun with new arguments afterwards.

build status

testling badge

Example

var debounce = require('async-debounce');

function async(num, done) {
  console.log('start', num);
  setTimeout(function() {
    console.log('done', num);
    done();
  }, 200);
}

var debounced = debounce(async, 50);

console.log('call 1'); debounced(1);
setTimeout(function() { console.log('call 2'); debounced(2) }, 100);
setTimeout(function() { console.log('call 3'); debounced(3) }, 200);

And in the output you can see that the function is run at max once at a time and if the debounce triggers while the function is still running, it will be queued.

$ node example.js
call 1
start 1
call 2
call 3
done 1
start 3
done 3

Installation

Install with npm:

$ npm install async-debounce

Install with component(1):

$ component install juliangruber/async-debounce

API

fn = debounce(fn, interval)

Returns a decorated version of fn that when called calls fn only when no further calls have happended for interval milliseconds and if it's not currently running. When it's done running and a call has happened while it was still running, it's called again with latest arguments.

fn will be given a callback as the last argument, do signal it's done with its computations.

License

MIT

async-debounce's People

Contributors

juliangruber avatar nateps avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.