Skip to content

Instantly share code, notes, and snippets.

@TorvaldsDB
Last active December 17, 2023 05:57
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 TorvaldsDB/e03915817f16b7fb377b5d70f5c28632 to your computer and use it in GitHub Desktop.
Save TorvaldsDB/e03915817f16b7fb377b5d70f5c28632 to your computer and use it in GitHub Desktop.
function debounce(func, delay) {
let timerId;
return function (...args) {
clearTimeout(timerId);
timerId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
// 使用用例
const debounceFunction = debounce(() => {
console.log("Debounced function executed.");
}, 500);
// 调用 debounce 函数返回的函数,会在最后一次调用后延迟 500 毫秒执行
debouncedFunction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment