1、common.js
//防抖节流
let timeout, result;
const debounce = function (func) {
let args = arguments;
console.log(args);
if (timeout) {
clearTimeout(timeout)
}
let callNow = !timeout
timeout = setTimeout(() => {
timeout = null;
}, 1000)
if (callNow) {
// result = func.apply(this, args) //如this指向有问题再开启 并注释下一行
result = func(...args)
}
return result
}
export default {
debounce
}
2、main.js
import global from '@/common/js/global.js'
Vue.prototype.$debounce = global.debounce // 防止多次点击事件
3、使用
<button class="cu-btn bg-blue shadow-blur round" @tap="$debounce(searchData,searchText)">搜索</button>