react 中使用 lodash 中的 _.throttle
1.场景: 首次调用执行一次,一定时间内再次调用,不再执行。 2.实现 debounce (函数去抖) 多次触发,只在最后一次触发时,执行目标函数。 _.debounce(func,[wait=0],[options={}]) throttle (函数节流)限制目标函数调用的频率,比如:1s内不能调用2次。 _.throttle(func,[options={}]) lodash在opitons参数中定义了一些选项,主要是以下三个: leading,函数在每个等待时延的开始被调用,默认值为false leading-false,trailing-true:默认情况,即在延时结束后才会调用函数 方式一: test = () => { console.log(‘--------------22222---------------‘); this.fun(); } fun = _.debounce(function(e){ console.log(‘--------------22222---------------‘); },5000,{ leading: true,trailing: false,}) 首次点击时执行,连续点击且时间间隔在5s之内,不再执行,间隔在5s之外再次点击,执行。 方式二: test = () => { console.log(‘--------------22222---------------‘); this.fun(); } fun = _.throttle(function(e){ console.log(‘--------------22222---------------‘); },}) 首次点击时执行,连续点击且间隔在5s之内,5s之后自动执行一次(注:连续点击次数时间之后小于5s,则不会自动执行),间隔在5s之外再次点击,执行。 . (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |