Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Created March 16, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelbarbosa/5fafee7b1e404794ab6a76a93edb1b8d to your computer and use it in GitHub Desktop.
Save joelbarbosa/5fafee7b1e404794ab6a76a93edb1b8d to your computer and use it in GitHub Desktop.
const button = document.querySelector('button');
button.addEventListener("click", () => resizeWithTroattle(10));
const resizeWithTroattle = throttle(executeResize, 3000);
window.addEventListener("resize", () => resizeWithTroattle('test'));
function executeResize(number) {
console.log("resize: " + number);
}
function throttle(fn, time) {
let flag = true;
return function() {
if (flag) {
fn.apply(this, arguments);
flag = false;
setTimeout(() => {
flag = true;
}, time);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment