button按钮 使用防抖函数

1.全局注册有个方法
  // 注册在main.js里面方法 
  debounce(func, delay) {
        // 定义一个变量用来存储定时器
        let timer = null;
        // 返回一个新的函数
        return function() {
            // 获取当前函数的执行上下文和参数
            let context = this;
            let args = arguments;
            // 如果已经存在定时器,就取消它
            if (timer) {
                clearTimeout(timer);
            }
            // 创建一个新的定时器,在延迟时间后执行要执行的函数
            timer = setTimeout(function() {
                func.apply(context, args);
            }, delay);
        }
    }
2.界面使用
<van-button :disabled="loading"  round block type="info" color="#2fb3a7" @click="$gt.debounce(()=> {saveData()},30)()">提交</van-button>
3.这个方法使用$gt下面的debounce的属性里面
// gt.js
const GT = {
    ...

猜你喜欢

转载自blog.csdn.net/weixin_42066070/article/details/131327399
今日推荐