vue如何使用节流和防抖

42 阅读1分钟

使用v-reduceExpenditure-button 控制节流 创建 utils工具 throttle.js /按钮节流/ const throttle = (func, delay) => { // 缓存一个定时器 let timer = null; // 这里返回的函数是每次用户实际调用的节流函数 return function (...args) { if (!timer) { //判断timer是否有值,如果没有则说明定时器不存在即可继续执行 timer = setTimeout(() => { //关 func.apply(this, arguments); timer = null; //开 }, delay); } }; }; export default throttle;

页面使用 throttle.js `

`

也可以使用监听事件 watch 创建 utils 工具 antiShake.js /输入框防抖/ const antiShake = function (fn, delay) { let timer = null; return function () { let content = this; let args = arguments; if (timer) { clearTimeout(timer); } timer = setTimeout(() => { fn.apply(content, args); }, delay); }; };

export default antiShake;

页面使用 工具 antiShake