Skip to content

Instantly share code, notes, and snippets.

@alivelee
Created May 16, 2021 10:59
Show Gist options
  • Save alivelee/04562d046c08f7c37b88ea4aaa5eda00 to your computer and use it in GitHub Desktop.
Save alivelee/04562d046c08f7c37b88ea4aaa5eda00 to your computer and use it in GitHub Desktop.
const debounce = (fn, ms = 0) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(
() => fn.apply(this, args),
ms
);
};
};
// 每250ms打印一次window尺寸
window.addEventListener(
'resize',
debounce(() => {
console.log(window.innerWidth);
console.log(window.innerHeight);
}, 250)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment