vue使用防抖

防抖节流在网上随便一搜一大堆,但是自己拿下来用总是不能实现,可能是我自己调用的方式不对,这是我自己后来改的:

方法一:(有局限性,不能把防抖提到common.js里面)

函数部分:

 //防抖函数
 debounce(fn, delay) {
     let _this = this
     return function() {
         if (_this.timer) {
             clearTimeout(_this.timer)
         }
         _this.timer = setTimeout(fn, delay)
     }
 },
 //测试防抖输出的
 fdfunc() {
     console.log("i am test the function of debounce")
 },
 //自己加的点击事件
 testfd() {
     this.debounce(this.fdfunc, 3000)()
 }

timer设置为了全局变量。

调用:

<el-button type="warning" @click="testfd">测试防抖</el-button>

方法二:

debounce(fn, delay) {
    let timer = null;
    return function() {
        clearTimeout(timer)
        timer = setTimeout(() => {
            fn.apply(this, arguments)
        }, delay)
    }
},

防抖函数这样写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值