一定时间点击提交按钮多次只触发一次
在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)
}
}
页面中使用
<template>
<button class="submit_btn" @tap="submitFeedback">提 交</button>
</template>
<script>
import {Debounce} from '@/utils/debounce.js'
export default {
data() {
return {}
},
methods: {
//提交
submitFeedback:Debounce(function(e) {
let _this = this;
this.$api.submitQuestion({
emphasisNameId: this.emphasisName.id,
syncId: uni.getStorageSync('sync_id')
}, res => {
if (res.code == 0) {
this.isComplete = true;
_this.openMpopup('success', '提交成功', 1500);
} else {
_this.openMpopup('err', res.msg, 1500);
}
})
},1000)
}
}
</script>