vue项目防抖节流插件的使用
lodash
- 安装 npm i --save lodash
- 在main.js引入 import _ from ‘lodash’
- 使用:
//防抖
//参数写在function里面,有async,await的 async 写在function前面
searchbtn:_.debounce(function(){
this.getdata()//更新视图
},300)//三百毫秒更新一次
//节流
[options.leading=true] (boolean): 指定调用在节流开始前,默认true。
[options.trailing=true] (boolean): 指定调用在节流结束后,默认true。
lodash默认trailing为true,那么最终的效果是在点击时会立即打印throttle,且5秒后又会再打印一次,即节流之前和之后都会执行该节流函数。
searchbtn:_.throttle(function(){
this.getdata()//更新视图
},5000, {
leading: true,
trailing: false
})
//三秒内再次触发会重新开始计算,三秒钟更新一次