vue防抖节流函数——组件封装,防止按钮多次点击

1.vue 封装utils.js

/**
 * @param {function} func 执行函数
 * @param {number} time 防抖节流时间
 * @param {boolean} isDebounce [1,3]为防抖组件,[2]为节流组件
 * @param {this} ctx this 的指向
*/
const debounce = (func, time, isDebounce, ctx) => {
    var timer, lastCall, rtn;
    // 防抖函数
    if (isDebounce == 1) {
        rtn = (...params) => {
            if (timer) clearTimeout(timer);
            timer = setTimeout(() => {
                func.apply(ctx, params);
            }, time);
        };
    } else if(isDebounce == 2){ // 节流函数
        rtn = (...params) => {
            const now = new Date().getTime();
            if (now - lastCall < time && lastCall) return;
            lastCall = now;
            func.apply(ctx, params);
        };
    } else if(isDebounce == 3){ // 立即执行的防抖函数
        rtn = (...params) => {
            if (timer) clearTimeout(timer);
            let callNow = !timer;
            timer = setTimeout(() => {
                timer = null;
            }, time)
            if (callNow) func.apply(ctx, params)
        };
    }
    return rtn;
};
 
export default {
    name: 'Debounce',
    abstract: true,
    props: {
        time: {
            type: Number,
            default: 800,
        },
        events: {
            type: String,
            default: 'click',
        },
        isDebounce: {
            type: Number,
            default: 1,
        },
    },
    created() {
        this.eventKeys = this.events.split(',');   // 分隔事件
        this.originMap = {};  // 储存事件,用于重新render时与子事件的对比
        this.debouncedMap = {};   // 储存防抖节流事件
    },
    render() {
        const vnode = this.$slots.default[0];
        this.eventKeys.forEach(key => {
            const target = vnode.data.on[key];
            if (target === this.originMap[key] && this.debouncedMap[key]) {
                vnode.data.on[key] = this.debouncedMap[key];
            } else if (target) {
                this.originMap[key] = target;
                this.debouncedMap[key] = debounce(
                    target,
                    this.time,
                    this.isDebounce,
                    vnode
                );
                vnode.data.on[key] = this.debouncedMap[key];  // 重写子组件的事件
            }
        });
        return vnode;
    },
};

2.在main.js入口文件里面全局注册

 // 防抖节流
 import Debounce from './assets/componentFn/utils'
  
 Vue.component('Debounce',Debounce)

3.使用方法

<!--当是isDebounce==1时表示是防抖函数,isDebounce==2是节流函数,isDebounce==3是立即执行版防抖函数,time是执行时间间隔,UI框架click事件失效时可用修饰符native-->
    <Debounce :time='2000' :isDebounce="2">
        <Button type="warning"  @click.native='btn'>btn</Button>
    </Debounce>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值