js 中封装节流函数
export function throttle(fn, delay = 5000) {
let timer
return function() {
let th = this,
args = arguments;
if (!timer) {
fn.call(th, args);
timer = setTimeout(function() {
timer = null;
}, delay)
}
}
}
export function debounce(fn, delay = 5000) {
let timer
return function() {
let th = this,
args = arguments;
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(function() {
fn.apply(th, args);
timer = null
}, delay)
}
}
使用
methods:{
testWechatSend: throttle(function () {
console.log('触发')
}, 1000)
}