主题: 防抖与节流

浏览器默认事件监听间隔: 4 - 6 ms, 对于某些场景不必要这么快速: 滚动,轮播, 点击 等

防抖: 高频操作只识别一次,(第一次或最后一次)

截流:对于高频操作 按照规定间隔出发,减少4 - 6 ms频率触发次数

防抖之最后一次 & 第一次

原始代码: 

var oBtn = document.getElementById('btn')

oBtn.onclick = function () {

        console.log('test')

}

思路分析: 

1. 需要: 原始function 

2. 触发click事件后进行控制, 需要: 原始function , 间隔时间, 开始/结束执行限制 , 这些控制本身需要一个function, 这个function : myDebounce(func, time, status)

3. myDebounce() 参数判断+默认值处理:  func 是否为函数, time 是否为数字, 是否为undefined,time 是否为boolean, status是否为boolean。 如果有问题 赋予默认值

4. 返回一个代理函数(内置定时器)

5. 执行定时器前清空定时器,抛弃之前的定时器, 只执行最后一个


6. this 指向 和 参数传递,  let self = this  &&   ...args 

7. 第一次触发执行, true ( 计时器 是否为空- 是否触发逻辑,传递参数是否为true-执行第一次出发逻辑)

var oBtn = document.getElementById('btn')
// handle 执行的函数, wait 等待时间, immediate: 1/0 - 开始/结束执行
function myDebounce (handle, wait, immediate){
    if(typeof handle !== 'function') throw new Error ('handle must be a function')  // 3. 判断参数类型 + 默认值处理
    if(typeof wait === 'undefined' ) wait = 300 
    if(typeof wait === 'boolean' ) {
        immediate = wait 
        wait = 300
    }
    // 惰性执行函数
    let timer = null                                                     
    return function proxy(...args) {             // 2. 既然会执行那就返回一个代理函数来执行 , 6.接受未知参数 ...args
        let self = this                          // 6. 储存this 
        clearTimeout(timer)                      // 5. 检查: 清楚之前的setTimeout, 只执行最后一个
        init =immediate && !timer
        timer = setTimeout(()=>{                 // 4. 这个函数 里 有一个定时器, 在wait后执行  // note:定时器的返回值是一个整数(定时器编号)
            timer = null
            !immediate ? handle.call('end',self, ...args) : null     // 6. 绑定this,使用参数
        },wait)
        init ? handle.call('start', self, ...args) : null 
    }
} 
function btnClick(ev){
    console.log(this, ev)
}
oBtn.onclick = myDebounce(btnClick, 300, true)  // 1. 这样会直接调用mydebounce

截流:自定义时间内让事件触发

原始代码: 

// css

body {

height: 1500px;

}

// js

function scrollFn(){

        console.log('gg')

}

window.onscroll = scrollFn

思路分析: 

1. 调用截流函数, 传入调用函数 和 间隔时间 => 返回一个代理函数

2. 参数判断  ?  确定传入了正确参数, 没有就加默认值

3. 逻辑判断: 预定时间差 - (当前时间 - 上次执行时间) => 大于0 则 高频 ,小于0 则 触发

4. 高频操作限制: a. 使用定时器 使 handle 在 指定时间执行 ,  b. “也就是说 高频触发的事件都会有一个定时器来延迟  c. 那么:  有定时器 = 已有一次高频触发”  d. 如果有高频出发了就不执行  e. if( !timer) {setTimeout(()=>{},200)}

5. 清空定时器 和 timer 变量: 为了下一次出设定定时器触发高频事件

6. 特殊情况: browser 和 自定义触发事件 时间点重合 那么定时器不会开启, 

clear Timeout (timer)

timer =null

// throttle
function throttle(handle, time){
    if(typeof handle !== 'function') throw new Error('handle must be a function')
    if(typeof time === 'undefined') wait = 400

    let previous  = new Date
    return function proxy(...args){
        let self = this
        let now = new Date()
        let difference = time - (now - previous)
        let timer = null 

        if(difference <= 0){
            clearTimeout(timer)
            timer = null
            handle.call(self, ...args)
            previous = new Date() 
        }else if(!timer) {
            timer = setTimeout(()=>{
                clearTimeout(timer)
                timer = null
                handle.call(self, ...args)
                previous = new Date()
            },difference)
        }
        
    }
}
function scrollFn(){
    console.log('gg')
}
// window.onscroll = scrollFn
window.onscroll =  throttle(scrollFn, 300)

  • 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、付费专栏及课程。

余额充值