1. debounce
Delay function calls until a set time elapses after the last invocation
debounce
Package: debounce
Created by: sindresorhus
Last modified: Fri, 17 Nov 2023 16:21:12 GMT
Version: 2.0.0
License: MIT
Downloads: 25,193,886
Repository: https://github.com/sindresorhus/debounce

Install

npm install debounce
yarn add debounce

debounce

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

Install

 npm install debounce

Usage

 import debounce from 'debounce';

function resize() {
	console.log('height', window.innerHeight);
	console.log('width', window.innerWidth);
}

window.onresize = debounce(resize, 200);

(You can also use const debounce = require('debounce'))

To later clear the timer and cancel currently scheduled executions:

 window.onresize.clear();

To execute any pending invocations and reset the timer:

 window.onresize.flush();

API

debounce(fn, wait, options?)

Creates a debounced function that delays execution until wait milliseconds have passed since its last invocation.

Set the immediate option to true to invoke the function immediately at the start of the wait interval, preventing issues such as double-clicks on a button.

The returned function has a .clear() method to cancel scheduled executions, and a .flush() method for immediate execution and resetting the timer for future calls.

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin