https://juejin.cn/post/7006257717820162056
防抖: 直接赋值方法到methods里面
debounce (func, delay = 1000, immediate = false) {
//闭包
let timer = null
//不能用箭头函数
return function () {
if (timer) {
clearTimeout(timer)
}
if (immediate && !timer) {
func.apply(this,arguments)
}
timer = setTimeout(() => {
func.apply(this,arguments)
}, delay)
}
}
//使用
<el-input v-model=“text” @input=“debounceInput” />
节流:直接复制到methods里面
export const throttle = (fn, delay = 1000) => {
//距离上一次的执行时间
let lastTime = 0
return function () {
let _this = this
let _arguments = arguments
let now = new Date().getTime()
//如果距离上一次执行超过了delay才能再次执行
if(now - lastTime > delay){
fn.apply(_this,_arguments)
lastTime = now
}
}
},
使用
<el-input v-model=“text” @input=“throttleInput” />