utils: type bounce arguments
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/pr/woodpecker Pipeline was successful Details

type-bounce-args
OFF0 1 year ago
parent 60510411a6
commit 2cdd09249f
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -5,19 +5,19 @@
* @param {*} time desired interval to execute function * @param {*} time desired interval to execute function
* @returns callback * @returns callback
*/ */
export const bounce = ( export const bounce = <T extends any[]>(
fn: () => void, fn: (...args: T) => void,
time: number, time: number,
) => { ) => {
let throttle: number | undefined; let throttle: number | undefined;
let debounce: number | undefined; let debounce: number | undefined;
return (/*...args*/) => { return (...args: T) => {
if (throttle) { if (throttle) {
clearTimeout(debounce); clearTimeout(debounce);
debounce = setTimeout(() => fn(/*...args*/), time); debounce = setTimeout(() => fn(...args), time);
return; return;
} }
fn(/*...args*/); fn(...args);
throttle = setTimeout(() => { throttle = setTimeout(() => {
clearTimeout(throttle); clearTimeout(throttle);
}, time); }, time);

Loading…
Cancel
Save