Socket
Socket
Sign inDemoInstall

@cdellacqua/debounce

Package Overview
Dependencies
2
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cdellacqua/debounce

Debounce a function with requestAnimationFrame or a custom interval


Version published
Weekly downloads
21
increased by61.54%
Maintainers
1
Install size
104 kB
Created
Weekly downloads
 

Readme

Source

Debounce a function with requestAnimationFrame or a custom interval

NPM Package

npm install @cdellacqua/debounce

Documentation

Highlights

import {debounce} from '@cdellacqua/debounce';

// It uses requestAnimationFrame by default
const debounced = debounce(() => {
	console.log(`Debounced with requestAnimationFrame: ${window.innerWidth}px x ${window.innerHeight}px`);
});

// It preserves function parameters
const debounced = debounce((a, b) => {
	console.log(`Debounced with requestAnimationFrame: ${a} and ${b} were passed`);
});
debounced("hello", "test argument");

// The interval can be customized, in this case setTimeout is used
const debounced = debounce(() => {
	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
}, 1000);

// Passing true as the third arguments makes the function call happen at the start
// of the interval: call then wait instead of wait then call
const debounced = debounce(() => {
	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
}, 1000, true);

// You can cancel or flush (call+cancel) the next scheduled execution
debounced.flush();
// ..or...
debounced.cancel();


// It exposes a state$ store containing 'idle' or 'debouncing'
const debounced = debounce(() => {
	console.log(`Debounced 1 second: ${window.innerWidth}px x ${window.innerHeight}px`);
}, 1000, true);

console.log(debounced.state$.content()); // 'idle'
debounced();
console.log(debounced.state$.content()); // 'debouncing'
// ...after 1000ms
debounced.state$.subscribe(console.log); // immediately prints 'idle'
debounced(); // triggers the subscription, printing 'debouncing'
// ...after 1000ms
// triggers the subscription, printing 'idle'

Keywords

FAQs

Last updated on 14 Jan 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc