Created
April 19, 2020 15:47
-
-
Save RuslanHolovko/e907ead40ae166a28b9787deba50d214 to your computer and use it in GitHub Desktop.
debounce function
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
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