防抖与节流

1、什么是防抖

搜索框 (执行次数太多,只执行最后一次)

每次搜索框输入时,都会发送一次请求,但每次都不是用户想要搜索的内容,等到用户输入完成时,再将搜索框的内容进行搜索,才是用户需要搜索的内容

1、2没有防抖的搜索过程 

当拼音输入前端开发时,发送了那么多次请求

 显然这都不是用户想要搜索的东西

<body>
    <input type="text" id="inp">
    <script>
        // 获取输入框
        let inp=document.getElementById('inp')
        // 用来存放定时器
        let t=null
        // 输入框监听输入的数据
        inp.oninput=function(){
           console.log(this.value)
        }
    </script>
</body>

1、3如何实现防抖

1、在每次输入文本后,判断在0.5秒内用户是否再次输入文本,

2、若用户在0.5s内没有输入文本,则把文本的内容 发送给服务器实现搜索

3、若用户在0.5s内继续输入文本,在输入后在判断用户是否输入,重复第二点

 定时器实现

<body>
    <input type="text" id="inp">
    <script>
        // 获取输入框
        let inp=document.getElementById('inp')
        // 用来存放定时器
        let t=null
        // 输入框监听输入的数据
        inp.oninput=function(){
            // 如果t是定时器
            if(t!==null){
                // 清除定时器
                clearTimeout(t)
            }
            // 重新赋值定时器
            t=setTimeout(()=>{
                // 业务逻辑
                console.log('请求的数据',this.value)
            },500)
        }
    </script>
</body>

实现结果如下图所示

 1、4用闭包实现防抖函数

<body>
    <input type="text" id="inp">
    <script>
        // 获取输入框
        let inp=document.getElementById('inp')
        // 输入框监听输入的数据
        inp.oninput=debounce(function(){
            console.log('发送请求',this.value)
        },500)
        // 封装一个防抖函数
        function debounce(fn,delay){
            // 用来存放定时器
            let t=null
            // 让输入框执行上面的函数
            return function(){
                // 如果t是定时器
                if(t!==null){
                    // 清除定时器
                    clearTimeout(t)
                }
                // 重新赋值定时器
                t=setTimeout(()=>{
                    // 业务逻辑,修改this指向
                    fn.call(this)
                },delay)
            }
        }
    </script>
</body>

2、什么是节流 

点击按钮(页面一下执行太多次, 控制执行次数)

 业务逻辑:点击按钮向服务器发送请求,又要避免用户多次点击按钮发送请求

// 获取btn按钮
let btn=document.getElementById('btn')
btn.onclick=throttle(function(){
    console.log('发送了请求')
},2000)

// 节流函数
function throttle(fn,delay){
    // 不会被干扰(闭包)
    let flag=true
    return function(){
        // 为true,发送一次请求
        if(flag){
            setTimeout(()=>{
                fn.call(this)
                flag=true
            },delay)
        }
        flag=false
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值