Last active
January 7, 2019 06:32
-
-
Save joshuacerbito/e7d8d769575ec630cfe7a8927ffb2896 to your computer and use it in GitHub Desktop.
Debounce + Throttle
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
export function debounce (fn, time) { | |
let timeout; | |
return function() { | |
const functionCall = () => fn.apply(this, arguments); | |
clearTimeout(timeout); | |
timeout = setTimeout(functionCall, time); | |
} | |
} | |
export function throttled (delay, fn) { | |
let lastCall = 0; | |
return function (...args) { | |
const now = (new Date).getTime(); | |
if (now - lastCall < delay) { | |
return; | |
} | |
lastCall = now; | |
return fn(...args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment