Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Created February 21, 2020 02:25
Show Gist options
  • Save elitenomad/4c1e4831a88814e3507d97a178fc9b2a to your computer and use it in GitHub Desktop.
Save elitenomad/4c1e4831a88814e3507d97a178fc9b2a to your computer and use it in GitHub Desktop.
simple implementation of debounce functionality (Type ahead functionality)
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