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