React事件中的防抖

防抖函数有很多应用场景,比如输入框change事件中请求后台数据, scroll事件加载更多等等一些高频发生的事件中。。

loadsh中的throttle函数

Creates a throttled function that only invokes func at most once per every wait milliseconds. The throttled function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the throttled function. Subsequent calls to the throttled function return the result of the last func invocation.
创建一个每wait毫秒最多只调用一次的函数。throttling函数带有一个取消方法来取消延迟的func调用,还有一个刷新方法来立即调用它们。提供选项,以指示是否应该在等待超时的前边缘和/或后边缘调用func。使用提供给throttling函数的最后一个参数调用func。对throttling函数的后续调用返回上次func调用的结果。

React里面如何使用Throttle?

  1. 众所周知, React里面的事件并非DOM的原生事件,并且它也没有真正绑定在DOM元素上,它是一个React经过合成优化的对象,所以不用考虑里面属性的兼容性
  2. react里面防抖

    handleChange = (function(){
    let func = lodash.throttle(function(e){
    console.log(e.target.value);
    }, 2000)
    return function(e){
    e.persist();
    func(e);
    }
    })()

转载于:https://www.cnblogs.com/paopaolee/p/9498600.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值