helpers/debounce.js

23 lines
428 B
JavaScript
Raw Normal View History

2022-03-18 12:54:58 +01:00
/**
@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);
};
};