浅谈vue中使⽤防抖与节流

15 篇文章 1 订阅

⼀、防抖
防抖(debounce):触发⾼频事件后 n 秒内函数只会执⾏⼀次,如果 n 秒内⾼频事件再次被触发,则重新计算时间。
  使⽤场景:频繁触发、输⼊框搜索等。
  具体⽅法如下:


export const debounce = (fn, t) => {
    let delay = t || 500;
    let timer;
returnfunction() {
        let args = arguments;
if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            timer = null;
            fn.apply(this, args);
        }, delay);
    };
};

⼆、节流
  节流(thorttle):⾼频事件触发,但在 n 秒内只会执⾏⼀次,所以节流会稀释函数的执⾏频率。
  使⽤场景:使⽤场景:频繁触发、onrize,onscroll滚动条,抢购按钮⾼频点击。


export const Throttle = (fn, t) => {
    let last;
    let timer;
    let interval = t || 500;
returnfunction() {
        let args = arguments;
        let now = + new Date();
if (last && now - last < interval) {
            clearTimeout(timer);
            timer = setTimeout(() => {
                last = now;
                fn.apply(this, args);
            }, interval);
        } else {
            last = now;
            fn.apply(this, args);
        }
    };
};

三、实现案例

<template>
    <div>
        <div>防抖</div>
        <input type='text' @keydown="hangleChange">
        <div>{{value}}</div>
        <div>节流</div>
        <input type='text' @keydown="hangleChange1">
        <div>{{value1}}</div>
    </div>
</template>
<script>
    import { debounce,Throttle } from '@/plugins/debounceThorttle.js'
    export default {
        name: 'test',
        data() {
return {
                value: '',
                value1: ''
            }
        },
        methods: {
            hangleChange: debounce(function(e){
this.value = e.target.value
            }),
            hangleChange1: Throttle(function(e) {
this.value1 = e.target.value
            })
        }
    }
</script>
<style>
</style>


四、区别:防抖动是将多次执⾏变为最后⼀次执⾏,节流是将多次
执⾏变成每隔⼀段时间执⾏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,有一些常用的防抖节流插件可以方便地实现防抖节流的功能。以下是两个常用的插件: 1. Lodash(防抖节流) Lodash是一个JavaScript实用工具库,它提供了许多常用的函数和方法,包括防抖节流函数。使用Lodash的`debounce`和`throttle`函数可以很方便地实现防抖节流。 安装Lodash: ```bash npm install lodash ``` 使用示例: ```javascript import { debounce, throttle } from 'lodash'; // 防抖示例 const debouncedFunc = debounce(() => { console.log('执行操作'); }, 500); // 节流示例 const throttledFunc = throttle(() => { console.log('执行操作'); }, 200); ``` 2. Vue-lodash(防抖节流Vue-lodash是一个专门为Vue开发的Lodash插件,它提供了Vue指令的方式来使用Lodash的方法,包括防抖节流。 安装Vue-lodash: ```bash npm install vue-lodash ``` 在Vue项目中使Vue-lodash示例: ```javascript import Vue from 'vue'; import VueLodash from 'vue-lodash'; import { debounce, throttle } from 'lodash'; Vue.use(VueLodash, { lodash: { debounce, throttle } }); ``` 在Vue组件中使防抖节流: ```html <template> <div> <button v-debounce:click="debouncedFunc">点击按钮(防抖)</button> <button v-throttle:click="throttledFunc">点击按钮(节流)</button> </div> </template> <script> export default { methods: { debouncedFunc() { console.log('执行操作'); }, throttledFunc() { console.log('执行操作'); }, }, }; </script> ``` 以上是两个常用的Vue插件,可以方便地在Vue项目中使防抖节流功能。根据具体需求选择合适的插件来使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值