有的时候网速不好,或者用户习惯不好的时候喜欢双击按钮,就会造成表单重复提交多次,看下图
然后添加了个节流函数,这样在短时间内点击的话,只会执行一次,上代码:
/*** 节流函数 在规定的时间内请求函数的次数* @param fn 需要执行的方法* @param interval 执行的间隔时间*/export function _throttle(fn, interval) {let last = 0let timerconst interVal = interval || 200return function() {const th = thisconst args = argumentsconst now = new Date().getTime()if (last && now - last < interVal) {clearTimeout(timer)timer = setTimeout(function() {last = new Date().getTime()// fn.apply(th, args)}, interVal)} else {last = new Date().getTime()fn.apply(th, args)}}}
<el-button type="primary" @click="clickCount">点击</el-button>methods: {clickCount: _throttle(function() {console.log(1)}, 1000)}