需求:请求防抖 函数立即执行 且在后续时间内不会再次执行
1.写一个common.js用来封装一下函数
export default {
data(){
return{
funcloading:false,
functimeout:undefined
}
},
methods: {
// 替换字符串中的空格和回车
replace_null(str) {
return str
.replace(/\ +/g, "")
.replace(/[ ]/g, "")
.replace(/[\r\n]/g, "");
},
// 请求防抖 函数立即执行 且在后续时间内不会再次执行
// 参数: 函数,等待时间,函数参数
modal_debounce(func, delay, funargs) {
if (this.funcloading == true) {
return;
} else {
if (this.functimeout) {
clearTimeout(this.functimeout);
}
if(funargs){
func(funargs);
}else{
func();
}
this.funcloading = true;
this.functimeout = setTimeout(() => {
this.funcloading = false;
}, delay);
}
}
}
}
2.在main.js中进行全局注册
import Commonjs from './components/common';
Vue.mixin(Commonjs);
3.在组件中使用方式
<a-button
class="filiter_item"
@click="modal_debounce(addtoindustrypool,1000)"
:disabled="selectedRowKeys.length == 0"
>添加到行业词库</a-button>