vue防止表单重复提交,节流函数

14 篇文章 0 订阅
14 篇文章 0 订阅

有的时候网速不好,或者用户习惯不好的时候喜欢双击按钮,就会造成表单重复提交多次,看下图

然后添加了个节流函数,这样在短时间内点击的话,只会执行一次,上代码:

/**
 * 节流函数 在规定的时间内请求函数的次数
 * @param fn  需要执行的方法
 * @param interval 执行的间隔时间
 */
export function _throttle(fn, interval) {
  let last = 0
  let timer
  const interVal = interval || 200
  return function() {
    const th = this
    const args = arguments
    const now = new Date().getTime()
    if (last && now - last < interVal) {
      clearTimeout(timer)
      timer = setTimeout(function() {
        last = new Date().getTime()
        // fn.apply(th, args)
      }, interVal)
    } else {
      last = new Date().getTime()
      fn.apply(th, args)
    }
  }
}
<el-button type="primary" @click="clickCount">点击</el-button>


methods: {
    clickCount: _throttle(function() {
      console.log(1)
    }, 1000)
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值