网上弄了个debounce,配上之后完全不起作用,还是重复提交
debounce(func, wait) {
let timer;
return function() {
let context = this;
let args = arguments;
if (timer) clearTimeout(timer);
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, wait)
if (callNow) func.apply(context, args);
}
},
//事件函数
submitForm(e) {
this.debounce(this.submit, 1000)(e)
},
submit(e) {
//这里还是重复执行呀
}
代码片段:https://developers.weixin.qq.com/s/OWSCFUmP7njf
debounce的网上参考文档:https://www.cnblogs.com/cc-freiheit/p/10827372.html
function debounce(func, wait) { let timer; return function() { let context = this; // 注意 this 指向 let args = arguments; // arguments中存着e if (timer) clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args) }, wait) } } Page({ submitForm(e) { this.submit(e) }, submit: debounce(function (e){ }, 1000) })
debounce 要在声明函数时调用