为了防止重复提交,我们一般会用防抖方法来实现,下面是我项目中使用的截图供大家参考
代码如下:
在全局utils文件中定义全局方法
/**
* 简易防抖函数
* @param {Function} func -防抖目标函数
* @param {Number}} gap - 防抖时间间隔
*/
export const debounce = (func, gap) => {
let timer
return function () {
timer && clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, arguments)
}, gap)
}
}
在业务.vue文件中使用
import { debounce } from '@/utils/index.js' //先引入全局防抖方法
mounted () {
this.debounceSearch = debounce(this.debounceSearch, 500)
},