JavaScript 防抖函数实现:简单易懂的代码示例
function debounce(func, delay) { let timer = null; return function() { const context = this; const args = arguments; clearTimeout(timer); timer = setTimeout(function() { func.apply(context, args); }, delay); } }
// 使用方式: function test() { console.log('防抖成功!'); } const debounceTest = debounce(test, 1000); debounceTest(); // 等待1秒后输出'防抖成功!' debounceTest(); // 取消前一次操作,重新等待1秒后输出'防抖成功!'
原文地址: https://www.cveoy.top/t/topic/mZWx 著作权归作者所有。请勿转载和采集!