Vue中怎么对事件进行防抖和节流操作?


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

// fn是我们需要包装的事件回调, delay是时间间隔的阈值

function throttle(fn, delay) {

  // last为上一次触发回调的时间, timer是定时器

  let last = 0, timer = null

  // 将throttle处理结果当作函数返回

   

  return function () {

    // 保留调用时的this上下文

    let context = this

    // 保留调用时传入的参数

    let args = arguments

    // 记录本次触发回调的时间

    let now = +new Date()

     

    // 判断上次触发的时间和本次触发的时间差是否小于时间间隔的阈值

    if (now - last < delay) {

    // 如果时间间隔小于我们设定的时间间隔阈值,则为本次触发操作设立一个新的定时器

       clearTimeout(timer)

       timer = setTimeout(function () {

          last = now

          fn.apply(context, args)

        }, delay)

    } else {

        // 如果时间间隔超出了我们设定的时间间隔阈值,那就不等了,无论如何要反馈给用户一次响应

        last = now

        fn.apply(context, args)

    }

  }

}

 

// 用新的throttle包装scroll的回调

const better_scroll = throttle(() => console.log('触发了滚动事件'), 1000)

 

document.addEventListener('scroll', better_scroll)

在 Vue 里使用 lodash 中的 Debouncing 和 Throttling

事件节流和防抖是提高性能或降低网络开销的好方法。虽然 Vue 1曾经支持对事件的节流和防抖,但是在Vue 2中为了保持核心的简单性,删除对事件的节流和防抖的支持。因此,在Vue 2对对事件进行防抖和节流我们可以使用 lodash 来做。

安装

可以通过 yarn 或 npm 安装 lodash。

1

2

3

4

# Yarn

$ yarn add lodash

# NPM

$ npm install lodash --save

注意:如果我们不想导入lodash的所有内容,而只导入所需的部分,则可以通过一些Webpack构建自定义来解决问题。 还可以使用lodash.throttlelodash.debounce等软件包分别安装和导入lodash的各个部分。

throttling 方法

要对事件进行节流处理方法非常简单,只需将要调用的函数包装在lodash的_.throttle函数中即可。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<template>

  <button @click="throttledMethod()">Click me as fast as you can!</button>

</template>

 

<script>

import _ from 'lodash'

 

export default {

  methods: {

    throttledMethod: _.throttle(() => {

      console.log('I get fired every two seconds!')

    }, 2000)

  }

}

</script>

debouncing 方法

尽管节流在某些情况下很有用,但一般情况我们经常使用的是防抖。 防抖实质上将我们的事件分组在一起,并防止它们被频繁触发。 要在Vue组件中使用节流,只需将要调用的函数包装在lodash的_.debounce函数中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<template>

  <button @click="throttledMethod()">Click me as fast as you can!</button>

</template>

 

<script>

import _ from 'lodash'

 

export default {

  methods: {

    throttledMethod: _.debounce(() => {

      console.log('I only get fired once every two seconds, max!')

    }, 2000)

  }

}

</script>

参考:

  • Throttling and Debouncing in JavaScript

  • The Difference Between Throttling and Debouncing

  • Examples of Throttling and Debouncing

  • Remy Sharp’s blog post on Throttling function calls

  • 前端性能优化原理与实践

相关推荐:

2020年前端vue面试题大汇总(附答案)

vue教程推荐:2020最新的5个vue.js视频教程精选

更多编程相关知识,请访问:编程入门!!

以上就是Vue中怎么对事件进行防抖和节流操作?的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

latex的文件如何转html

javascript怎么设置页面标题

html如何只显示部分td文字

javascript如何去掉字符串重复值

typescript和javascript有什么区别

html中怎么隐藏列表

javascript作用域的全面解析(附代码)

html页面跳转及参数传递问题

如何用javascript实现手势库

怎么用html设置背景音乐

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...