//debounce.js
// 防抖 防止表单重复提交
export const Debounce = (fn, t) => {
let delay = t || 300
let timer
return function () {
let args = arguments
if (timer) {
clearTimeout(timer)
}
let callNow = !timer
timer = setTimeout(() => {
timer = null
}, delay)
//改变this指向
if (callNow) fn.apply(this, args)
}
}
Vue文件中使用
<script>
import { Debounce } from '@/utils/debounce'
export default{
methods:{
tapSearch:Debounce(function(){
console.log('内容触发查询了');
})
}
}
</script>