Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Created February 21, 2020 02:25
Show Gist options
  • Save elitenomad/6bb97ab778568995870d44cc1b090905 to your computer and use it in GitHub Desktop.
Save elitenomad/6bb97ab778568995870d44cc1b090905 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