debouce:封装防抖函数
let timeout = null
export function debounce(fn, wait) {
clearTimeout(timeout)
let self = this
let args = arguments
timeout = setTimeout(() => {
console.log('1')
fn.apply(self, args)
}, wait)
}
使用
输入<input @input="handle"></input>
handle: debounce((val)=>{
console.log('ceshi', val)
}, 1000),
debounceFn 封装
export function debounced(fn, wait) {
let timer
const rtn = function() {
clearTimeout(timer)
let self = this
let args = arguments
timer = setTimeout(() => {
fn.apply(self, args)
}, wait)
}
return rtn
}
使用
输入<input @input="handleValue"></input>
handleValue(){
const fn = debounceFn((val)=>{
console.log('测试', val)
},1000)
}
TODO:有时间补TypesScript版