Skip to content

Instantly share code, notes, and snippets.

@cyan33
Created April 27, 2018 05:29
Show Gist options
  • Save cyan33/a52d323b1f58f1f854f613b914307e89 to your computer and use it in GitHub Desktop.
Save cyan33/a52d323b1f58f1f854f613b914307e89 to your computer and use it in GitHub Desktop.
debounce
function debounce(fn){
var id, timestamp;
var wait = 99;
var check = function(){
var last = (Date.now()) - timestamp;
if (last < wait) {
id = setTimeout(check, wait - last);
} else {
id = null;
fn();
}
};
return function(){
timestamp = Date.now();
if(!id){
id = setTimeout(check, wait);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment