Created
February 21, 2020 02:25
-
-
Save elitenomad/4c1e4831a88814e3507d97a178fc9b2a to your computer and use it in GitHub Desktop.
simple implementation of debounce functionality (Type ahead functionality)
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
function debounce(fn, time) { | |
let setTimeOutId; | |
const context = this; | |
return function() { | |
if(setTimeoutId) { | |
clearTimeout(setTimeoutId); | |
} | |
setTimeoutId = setTimeout(function(){ | |
fn.apply(context, arguments); | |
setTimeoutId = null; | |
}, time) | |
} | |
} | |
function throttle(fn, time) { | |
let setTimeOutId; | |
const context = this; | |
return function() { | |
if(setTimeoutId) { | |
return; | |
} | |
setTimeoutId = setTimeout(function(){ | |
fn.apply(context, arguments); | |
setTimeoutId = null; | |
}, time) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment