GithubHelp home page GithubHelp logo

debounce's Introduction

debounce

debounce is a javascript debouncer (and throttle) that instead returning a debounced function it will execute it after the specified time.

  • ๐Ÿ˜ƒ Easy to use.
  • ๐Ÿš€ Lightweight (1.6 KB)
  • โšช๏ธ Zero dependencies.

โฌ‡๏ธ Import

const debounce = require("@icaruk/debounce");

๐Ÿงญ Usage:

debounce(wait, fnc, id, reverse);
  • wait (number) Number of miliseconds that must pass before the function executes.
  • fnc (function) Function that will be executed.
  • id (string) Unique identifier of the performed action. If the id is ommited the fnc argument will be stringified and used as id (less optimal).
  • reverse (boolean) Default false. If true it will throttle instead debounce.

๐Ÿ”ฎ Examples:

Debounce without id

const hello = (name) => {
	console.log( "Hello!" );
};

debounce(1000, hello);
debounce(1000, hello);
debounce(1000, hello);

// > Outputs:
// Hello!

Debounce with id

const hello = () => {
	console.log( "Hello!" );
};

debounce(1000, hello, "button");
debounce(1000, hello, "button");
debounce(1000, hello, "button_2");

// > Outputs:
// Hello!
// Hello!

Throttle with id

const hello = (name) => {
	console.log( `Hello ${name}!` );
};

debounce(1000, () => hello(0), "button", true);
debounce(1000, () => hello(1), "button", true);
debounce(1000, () => hello(2), "button", true);

// > Outputs:
// Hello 0!

๐Ÿ”ฎ Passing arguments:

โœ… GOOD

const hello = (name = "") => {
	console.log( `Hello ${name}!` );
};

debounce(1000, () => hello("Mike"));
debounce(1000, () => hello("Bob"));
debounce(1000, () => hello("Susan"));

// > Outputs:
// Hello Mike!
// Hello Bob!
// Hello Susan!

โŒ BAD

const hello = (name = "") => {
	name = " " + name;
	console.log( `Hello ${name}!` );
};

debounce(1000, hello("Mike")); // โŒ
debounce(1000, hello("Bob")); // โŒ
debounce(1000, hello("Susan")); // โŒ

// > Outputs:
// Hello!
// [Error] debounce: fnc is null
// Hello!
// Hello!
// [Error] debounce: fnc is null
// [Error] debounce: fnc is null

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.