Skip to content

Instantly share code, notes, and snippets.

@RuslanHolovko
Created April 19, 2020 15:47
Show Gist options
  • Save RuslanHolovko/e907ead40ae166a28b9787deba50d214 to your computer and use it in GitHub Desktop.
Save RuslanHolovko/e907ead40ae166a28b9787deba50d214 to your computer and use it in GitHub Desktop.
debounce function
const btn = document.querySelector('.btn');
function debounce (fn, delay) {
let timeout;
return function(){
if (timeout){
clearTimeout(timeout);
}
timeout = setTimeout(()=>{
fn()
},delay);
}
};
btn.addEventListener('click', debounce(()=>{
console.log('click');
},2000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment