Vue项目按钮防抖

Vue项目按钮防抖

防抖代码

// 防抖
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)
    }
}

页面引入

import { Debounce } from '@/utils/Debounce '

使用方法

methods: {
    Submit: Debounce(function () {
      this.formData.fullname = this.fullname;
      this.formData.sex = this.sex;
      this.formData.count++
    }, 3000)
  }

防止重复提交

你可能感兴趣的:(Vue项目按钮防抖)