首先新增一个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)
if (callNow) fn.apply(this, args)
}
}
在组件里面移入该文件
import { Debounce } from "@/utils/debounce";
在提交事件函数加上这个方法,首次点击提交按钮会立即执行一次Debounce方法,后面3s内不触发事件才能继续执行。这很适合防止表单重复提交
queryData: Debounce(function() {
// 这里写入要操作的逻辑 ,表单提交或者查询都可以
}, 1000),