export const debounce = (fn, delay = 1000) => {
let timer = null;
return function () {
if (timer) {
clearTimeout(timer);
timer = null
}
timer = setTimeout(() => {
fn.apply(this, arguments);
timer = null;
}, delay)
}
}
export const throttle = (fn, delay = 1000) => {
let _arguments = arguments
let canRun = true
return function () {
if (!canRun) return
canRun = false
setTimeout(() => {
fn.call(this, _arguments)
canRun = true
}, delay);
}
}
用法
fun:throttle(function(){
...
},5000),
//如果带参数的话
fun:throttle(function(e){
console.log(e)
},5000),
ps:在编辑器中fun的颜色可能会跟其他的方法名不一样,但是是可以用的