常用的表單提交中,如果不做按鈕loading的處理,就需要給按鈕配置防抖操作。
廢話不多說,上代碼:
第一步,以下代碼寫到工具類裏面;
// 防抖
export const DebounceBy = (fn, t) => {
let delay = t || 500
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)
}
}
第二步:在需要用到的地方,引入:
第三步,在method的方法裏面直接使用
agree:DebounceBy(function(value){
/*寫你自己的業務代碼*/ },3000)
完畢