Add debounce

This commit is contained in:
trading_peter 2022-03-18 12:54:58 +01:00
parent 2ee8d505fe
commit 6620648bf0
2 changed files with 23 additions and 1 deletions

22
debounce.js Normal file
View File

@ -0,0 +1,22 @@
/**
@license
Copyright (c) 2022 trading_peter
This program is available under Apache License Version 2.0
*/
export const debounce = (cb, delay, immediate) => {
let timeout;
return (...a) => {
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
if (!immediate) cb.apply(this, a);
}, delay);
if (callNow) cb.apply(this, a);
};
};

View File

@ -1,6 +1,6 @@
{ {
"name": "@tp/helpers", "name": "@tp/helpers",
"version": "1.0.1", "version": "1.0.2",
"description": "", "description": "",
"main": "closest.js", "main": "closest.js",
"scripts": { "scripts": {