防抖和节流

卡顿现象:

正常:事件触发非常频繁,每一次触发,回调函数都会执行,如果时间很短,回调函数中有计算,有可能会出现浏览器卡顿

节流:(执行一次,规定的时间内不再触发)在规定的时间间隔范围内,不会触发多次回调,只有大于这个时间才会触发回调。频繁触发 => 少量触发

防抖:(重置时间执行最后一次)前面所有的触发都会取消,最后一次的执行在规定的时间之后才会触发 频繁触 => 只执行一次

方式一: lodash插件:

lodash向外暴露的是_.+函数名()

防抖: _.debounce(fn,waitTime,option)

节流: _.throttle(fn,waitTime,option)

npm i --save lodash   // 安装第三方插件

import _ from 'lodash'
    export default {
			methods: {
				//触发的事件   这里不能用箭头函数  因为箭头函数没有自身的this 
			      scrolls:_.debounce(function (event) {
			          console.log(event.target.scrollTop)
			          if(event.target.scrollTop > 0){
			              this.changeStyle = true;
			          }else{
			              this.changeStyle = false;
			          }
			      }, 200)
			}
    }

深入解析: 深入篇 | Lodash 防抖和节流是怎么实现的_51CTO博客_vue lodash防抖

方式二: 定时器和高阶函数(手写):

/**
 * 第三个参数可控制是首次触发还是时间结束触发
 * @param {Function} func
 * @param {Number} wait
 * @param {Boolean} immed
 * @returns
 */
function debounce (func, wait, immed) {
  let timer
  return function (...params) {
    if (timer) clearTimeout(timer)
    if (immed) {
      let flag = !immed
      timer = setTimeout(() => {
        timer = null
      }, wait)
      if (flag) func.apply(this, params)
    } else {
      timer = setTimeout(() => {
        func.apply(this, params)
      }, wait)
    }
  }
}

/**
 *
 * @param {Function} func
 * @param {Number} wait
 * @returns
 */
function throttle (func, wait) {
  let sta = 0
  let timer
  return function (...args) {
    let nowTime = new Date().getTime()
    let res = wait - (nowTime - sta)
    if (timer) clearTimeout(timer)
    if (res <= 0) {
      func.apply(this, args)
      sta = nowTime
    } else {
      timer = setTimeout(() => {
        func.apply(this, args)
        sta = nowTime
      }, res)
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值