lodash防抖与节流使用

说明:

防抖:n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时 
// 防抖
const res = _.debounce(this.handleConsole, 2000);

节流:n 秒内只运行一次,若在 n 秒内重复触发,只有一次生效
// 节流
const res1 = _.throttle(this.handleConsole, 2000);

handleClick = () => {
    // 首次点击时执行,连续点击且时间间隔在1s之内,不再执行,间隔在1s之外再次点击,执行。
    this.debounce();
    // 首次点击时执行,连续点击且事件间隔在1s之内,1s之后自动执行一次(注:连续点击次数时间之后小于1s,则不会自动执行),间隔在1s之外再次点击,执行。
    this.throttle();
}

debounce = _.debounce(this.handleConsole, 1000, {
    leading: true,
    trailing: false
});
 
throttle = _.throttle(this.handleConsole, 1000, {
  leading: true,
  trailing: false,
});

// 不能使用箭头函数,会报错
handleConsole () {
    console.log(111);
}

用法:


// 防抖:n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时 
const handleSelectChange = _.debounce(
    async item => {
        const type = item.key;
        // 不存在数字人不跳转界面
        const __loginInfo = Taro.getStorageSync('__loginInfo');
        const [_, resp] = await promiseTo(designServices.fetchMallList({customerId: __loginInfo.pcInfo.customerId, compId: __loginInfo.pcInfo.compId}));
        if (!resp.bean || !resp.bean.digitalHumanId) {
            return showToast('mall-sensitive-word', 'fail', `请联系客服购买数字人`);
        }

        const curVideoInfo = await handleAddAudioInfo(type);
        if (curVideoInfo) {
            Taro.navigateTo({
                url: `/mod-design/pages/video-creator/index?type=${type}&curVideoInfo=${encodeURIComponent(
                    JSON.stringify(curVideoInfo)
                )}&creationType=${''}&from=detect&detectContent=${encodeURIComponent(editorContent ? editorContent.trim() : '')}`
            });
        }
        handleHideTooltip();
    },
    3000,
    {leading: true, trailing: false}
);


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值