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

1.0.1 • Public • Published

Debounce

A simple utility to debounce a function.

Usage

Install package

npm i @kreisler/debounce

Import module

import { debounce } from "@kreisler/debounce";

or

Import commonjs

const { debounce } = require("@kreisler/debounce");

Example #1

const { debounce } = require("@kreisler/debounce");
const myFunction = (message) => alert(message);
const debouncedFunction = debounce(myFunction, 1000);
debouncedFunction("Hello World"); // Will alert "Hello World" after 1 second
debouncedFunction("Hello World"); // Will cancel the previous call and alert "Hello World" after 1 second
debouncedFunction("Hello World"); // alert "Hello World" after 1 second

Example #2

import { debounce } from "@kreisler/debounce";
const myFunction = (message) => alert(message);
const debouncedFunction = debounce(myFunction, 1000, { 
      immediate: true,
      onCall: (message) => console.log(`Calling function with message: ${message}`),
      onComplete: (message) => console.log(`Function completed with message: ${message}`)
      flood: 7,
      onFlood: (message) => console.log(`Function flooded 7 with message: ${message}`)
    });
debouncedFunction("Hello World"); // Will alert "Hello World" immediately // onCall will be called
debouncedFunction("Hello World"); // Will cancel the previous call and alert "Hello World" after 1 second // onCall will be called
debouncedFunction("Hello World"); // alert "Hello World" after 1 second // onCall will be called
debouncedFunction("Hello World"); // alert "Hello World" after 1 second // onCall will be called
debouncedFunction("Hello World"); // alert "Hello World" after 1 second // onCall will be called
debouncedFunction("Hello World"); // alert "Hello World" after 1 second // onCall will be called
debouncedFunction("Hello World"); // onFlood will be called // onCall will be called
debouncedFunction("Hello World"); // alert "Hello World" after 1 second // onComplete will be called

Package Sidebar

Install

npm i @kreisler/debounce

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

9.72 kB

Total Files

6

Last publish

Collaborators

  • kreisler