Last active
May 20, 2020 01:46
-
-
Save Grohden/9d69c93aaad448a88c12519c80b2caa1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// calls always the last | |
export const debounce = (time: number) => { | |
let timeout: ReturnType<typeof setTimeout> | null = null | |
return (fn: () => void) => { | |
if(timeout !== null) { | |
clearTimeout(timeout) | |
timeout = null | |
} | |
timeout = setTimeout(() => { | |
timeout = null | |
fn() | |
}, time) | |
} | |
} | |
// Executes within interval, ignores subsequent calls until first interval is reached | |
export const callUpdatedInInterval = (time: number) => { | |
let timeout: ReturnType<typeof setTimeout> | null = null | |
let currentFn: null | (() => void) = null | |
return (fn: () => void) => { | |
currentFn = fn | |
if(timeout !== null) { | |
return | |
} | |
timeout = setTimeout(() => { | |
timeout = null | |
currentFn?.() | |
currentFn = null | |
}, time) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment