Add debounce
This commit is contained in:
parent
2ee8d505fe
commit
6620648bf0
22
debounce.js
Normal file
22
debounce.js
Normal 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);
|
||||||
|
};
|
||||||
|
};
|
@ -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": {
|
||||||
|
Loading…
Reference in New Issue
Block a user