電站新增時點擊提交按鈕,手速快的童鞋會提交多幾個電站出來。為了解決這個問題,可以在提交按鈕上做個防抖操作,下面直接上代碼。
1.在根目錄工具包utils里新建debounce.js文件
export const Debounce = (fn, wait) => { let delay = wait|| 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) } }
2.在add.vue里引用以上debounce.js
import { Debounce } from '@/utils/debounce.js'
3.在method添加方法
formSubmit: Debounce(function(e) { console.log('form發生了submit事件,攜帶數據為:' + JSON.stringify(e.detail.value)) var formdata = e.detail.value formdata["image"] = uni.getStorageSync('uploadImg'); this.addStation(formdata) },1000),
4.頁面提交按鈕

<template>
<view class="contn">
<!-- <page-head :title="ttitle"></page-head> -->
<form @submit="formSubmit">
<view class="ssm">
<button class="logi" form-type="submit">
<!-- 提交 -->
<view class="logit">
{{i18n.SBD_Submit}}
</view>
</button>
</view>
</form>
</view> </template>
