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>
标签:防抖,vue,..,antiShake,timer,let,按钮
From: https://www.cnblogs.com/chenxdnote/p/16969302.html