Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

debounce

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

debounce

Delay function calls until a set time elapses after the last invocation

  • 2.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.7M
decreased by-16.91%
Maintainers
1
Weekly downloads
 
Created

What is debounce?

The debounce npm package provides a function that delays the execution of a function until after a specified wait time has elapsed since the last time it was invoked. This is particularly useful for rate-limiting the execution of functions that may be called frequently, such as during window resizing, scrolling, or keyboard input, to improve performance and reduce the number of unnecessary function calls.

What are debounce's main functionalities?

Debouncing function calls

This code sample demonstrates how to use the debounce package to debounce a function that logs 'Input saved!' to the console. The function will only be called after 250 milliseconds have passed without the 'input' event being triggered.

const debounce = require('debounce');
const saveInput = debounce(() => console.log('Input saved!'), 250);
window.addEventListener('input', saveInput);

Immediate invocation option

This code sample shows how to use the debounce package with the immediate flag set to true. This causes the function to be executed immediately on the first call, then debounced for subsequent calls within the 250-millisecond timeframe.

const debounce = require('debounce');
const processChange = debounce(() => console.log('Change processed!'), 250, true);
window.addEventListener('change', processChange);

Canceling a debounced call

This code sample illustrates how to cancel a debounced function call. The 'cancel' method is used to prevent the debounced function from being called if it has not yet been executed.

const debounce = require('debounce');
const expensiveOperation = debounce(() => console.log('Operation executed!'), 1000);
expensiveOperation();
expensiveOperation.cancel();

Other packages similar to debounce

Keywords

FAQs

Package last updated on 14 Oct 2024

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc