vue 项目中实现按钮防抖

1.新建 .js文件存放防抖方法

// 防抖
export const antiShake= (fn, t) => {
  let delay = t || 1000
  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.引入防抖文件,methods中添加方法

//引入防抖文件
import { antiShake } from '../../../../common/antiShake.js'; //防抖

methods: {  
         //给按钮添加防抖
        startDrawPolygon: antiShake(function () {
            this.getDepartments() //按钮触发的方法
        }),
}


3.html代码

<el-button @click="startDrawPolygon()" style="background-color:#409EFF; color: #FFF;" slot="append" icon="el-icon-search">搜索</el-button>
posted @   夜久听山雨  阅读(678)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示