vue2项目之防抖与节流的实现

防抖与节流(lodash.js)

我们这里用到一个第三方库:lodash.js

中文文档: https://www.lodashjs.com/docs/lodash.throttle

CDN:

<script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.corejs"></script>

vue里面安装及使用:

npm i lodash
import {debounce,throttle} from 'lodash'

methods: {
    addColor: throttle(function(index) {
        this.currentIndex = index;
    },50)
},
5.7.1 防抖

1、概念:前面的所有的触发都被取消,最后一次执行规定的时间 之后才会触发,也就是说如果连续快速的触发,只会执行最后一次

2、loadsh 中的 debounce 函数,参数为一个函数和防抖时间

3、一般用于表单的输入而触发的回调函数,实现当用户输入完成之后再执行回调函数

4、具体案例:当输入停止 且1s后 才会打印111

const input = document.querySelector('input');

input.oninput = _.debounce(function() {
    console.log('111');
},1000)
5.7.2 节流

1、概念:在规定的 间隔时间 内不会重复触发回调,只有大于这个时间间隔才会触发回调,把频繁触发变为少量触发

2、一般防止用户快速点击,例如1s内只能执行函数一次

3、loadsh 中的 throttle 函数,参数为一个函数和节流时间

4、使用(注意这里的 methods 函数使用方法)

import {throttle} from 'lodash'

methods: {
    addColor: throttle(function(index) {
        this.currentIndex = index;
    },50)
},
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue2中实现防抖节流可以使用Vue的指令和方法来实现。 ## 防抖 防抖实现方法是在一定时间内只执行一次方法,如果在这段时间内又有触发事件的操作,那么计时器就会被重新计时,直到时间结束才会执行方法。 ### 指令实现Vue2中可以使用v-debounce指令来实现防抖: ```html <template> <button v-debounce:click="handleClick">Click me</button> </template> <script> export default { methods: { handleClick() { console.log('Clicked'); } } } </script> ``` 可以看到v-debounce指令绑定在click事件上,这样就会在点击按钮时执行handleClick方法,但是在执行handleClick方法之前会先执行防抖函数。 下面是v-debounce指令的实现: ```javascript Vue.directive('debounce', { inserted: function (el, binding) { let timer; el.addEventListener('click', () => { if (timer) clearTimeout(timer); timer = setTimeout(() => { binding.value(); }, 500) }) } }) ``` 上面的代码实现了一个叫做debounce的指令,它会在元素插入到DOM中时绑定一个click事件,在click事件中执行防抖函数,最后执行传入的回调函数。 ### 方法实现 除了指令,我们也可以在Vue2的方法中实现防抖。 ```html <template> <button @click="debounceClick">Click me</button> </template> <script> export default { methods: { debounce(fn, delay) { let timer; return function () { if (timer) clearTimeout(timer); timer = setTimeout(() => { fn.apply(this, arguments); }, delay) } }, handleClick() { console.log('Clicked'); }, debounceClick: debounce(this.handleClick, 500) } } </script> ``` 上面的代码中我们在Vue2的methods中实现了一个debounce方法,它返回一个函数,这个函数会在delay时间内执行一次fn方法。我们在handleClick方法前面加上了debounce,这样每次点击按钮时就会执行debounceClick方法,而不是直接执行handleClick方法。 ## 节流 节流实现方法是在一段时间内只执行一次方法,如果在这段时间内有多次触发事件的操作,那么只有第一次触发事件的操作会执行方法,其余的操作会被忽略,直到时间结束才会重新计时。 ### 指令实现Vue2中可以使用v-throttle指令来实现节流: ```html <template> <button v-throttle:click="handleClick">Click me</button> </template> <script> export default { methods: { handleClick() { console.log('Clicked'); } } } </script> ``` 可以看到v-throttle指令绑定在click事件上,这样就会在点击按钮时执行handleClick方法,但是在执行handleClick方法之前会先执行节流函数。 下面是v-throttle指令的实现: ```javascript Vue.directive('throttle', { inserted: function (el, binding) { let timer; el.addEventListener('click', () => { if (!timer) { timer = setTimeout(() => { timer = null; binding.value(); }, 500) } }) } }) ``` 上面的代码实现了一个叫做throttle的指令,它会在元素插入到DOM中时绑定一个click事件,在click事件中执行节流函数,最后执行传入的回调函数。 ### 方法实现 除了指令,我们也可以在Vue2的方法中实现节流。 ```html <template> <button @click="throttleClick">Click me</button> </template> <script> export default { methods: { throttle(fn, delay) { let timer; return function () { if (!timer) { timer = setTimeout(() => { timer = null; fn.apply(this, arguments); }, delay) } } }, handleClick() { console.log('Clicked'); }, throttleClick: throttle(this.handleClick, 500) } } </script> ``` 上面的代码中我们在Vue2的methods中实现了一个throttle方法,它返回一个函数,这个函数会在delay时间内执行一次fn方法。我们在handleClick方法前面加上了throttle,这样每次点击按钮时就会执行throttleClick方法,而不是直接执行handleClick方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cocoonne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值