2020-07-01发表2020-07-01更新Vue1 分钟读完 (大约132个字)Vue 防抖使用方法防抖函数实现方式一:多次点击只执行最后一次12345678910/* 多次点击 只执行最后一次 */const debounce = (function () { let timer = null; return function (func, delay = 1000) { if (timer) { clearTimeout(timer); } timer = setTimeout(func, delay); };})(); 方式二:先执行一次123456789101112131415161718192021222324/* 先执行一次 */const debounce2 = (function () { let timer = null; let flag = true; return function (func, delay = 1000) { if (flag) { flag = false; func.call(); return; } if (timer) { clearTimeout(timer); } timer = setTimeout(() => { flag = true; func.call(); }, delay); };})();export default { debounce, debounce2,}; 挂载到 Vue 原型12345/* 挂载到原型 */import debounce from "./debounce/debounce";Vue.prototype.$utils = { ...debounce,}; 使用示例1234/* 使用 */this.$utils.debounce(() => { // ... }, 500); #JavaScriptVue前端随笔