表单数据提交,多次点击提交按钮便会发起多次请求,造成不必要的错误!
debounce.js
// debounce.js
export function _debounce(fn, delay) {
var delay = delay || 600;
var timer;
let flag = true;
return function(th, args) {
var th = this;
var args = arguments;
if (flag) {
fn.apply(th, args);
flag = false;
}
clearTimeout(timer);
timer = setTimeout(function() {
flag = true;
}, delay);
};
}
vue项目调用
import { _debounce } from "@/js/debounce";
export default {
name: 'HolloWorld',
methods: {
// 提交账号信息
submitBtn: _debounce(function() {
this.api_submitAccount()
}),
// 接口调用:提交账号信息
api_submitAccount () {
}
}
}