Socket
Socket
Sign inDemoInstall

debounce-fn

Package Overview
Dependencies
1
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

debounce-fn


Version published
Maintainers
1
Install size
9.35 kB
Created

Package description

What is debounce-fn?

The debounce-fn npm package provides a simple and efficient way to debounce functions in JavaScript. Debouncing is a technique used to limit the rate at which a function is executed. This is particularly useful for performance optimization in scenarios where a function is called repeatedly in quick succession, such as during window resizing, scrolling, or keypress events.

What are debounce-fn's main functionalities?

Basic Debouncing

This feature allows you to debounce a function with a specified wait time. In this example, the `expensiveFunction` will only be called once every 200 milliseconds, even though `debouncedFunction` is called every 50 milliseconds.

const debounceFn = require('debounce-fn');

const expensiveFunction = () => {
  console.log('Expensive function called');
};

const debouncedFunction = debounceFn(expensiveFunction, { wait: 200 });

// Call the debounced function multiple times
setInterval(debouncedFunction, 50);

Immediate Execution

This feature allows the debounced function to be executed immediately on the first call, and then debounced for subsequent calls. In this example, `logMessage` is executed immediately on the first call, and then debounced for 200 milliseconds.

const debounceFn = require('debounce-fn');

const logMessage = () => {
  console.log('Function executed immediately');
};

const debouncedLogMessage = debounceFn(logMessage, { wait: 200, immediate: true });

debouncedLogMessage(); // Executes immediately
setTimeout(debouncedLogMessage, 100); // Will not execute
setTimeout(debouncedLogMessage, 300); // Executes after 300ms

Cancel Debounced Function

This feature allows you to cancel a debounced function before it gets executed. In this example, `fetchData` is debounced with a wait time of 300 milliseconds, but the second call to `debouncedFetchData.cancel()` cancels the execution.

const debounceFn = require('debounce-fn');

const fetchData = () => {
  console.log('Fetching data');
};

const debouncedFetchData = debounceFn(fetchData, { wait: 300 });

debouncedFetchData();
debouncedFetchData.cancel(); // Cancels the debounced function

Other packages similar to debounce-fn

Readme

Source

debounce-fn Build Status

Debounce a function

Install

$ npm install debounce-fn

Usage

const debounceFn = require('debounce-fn');

window.onresize = debounceFn(() => {
	// Do something on window resize
}, {wait: 100});

API

debounceFn(input, [options])

Returns a debounced function that delays calling the input function until after wait milliseconds have elapsed since the last time the debounced function was called.

It comes with a .cancel() method to cancel any scheduled input function calls.

input

Type: Function

Function to debounce.

options

Type: Object

wait

Type: number
Default: 0

Time to wait until the input function is called.

immediate

Type: boolean
Default: false

Trigger the function on the leading edge instead of the trailing edge of the wait interval. For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.

  • p-debounce - Debounce promise-returning & async functions

License

MIT © Sindre Sorhus

Keywords

FAQs

Last updated on 10 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc