VUE使用节流和防抖

文章介绍了JavaScript中用于优化性能的节流和防抖技术,包括它们的概念、具体实现以及应用场景。提供了自定义实现节流和防抖的函数代码,并展示了如何在Vue项目中使用。此外,还提到了Lodash库作为实现节流和防抖的另一种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、自己封装方法实现:

1、何谓节流和防抖?

  • 节流
    节流的意思是,规定时间内,只触发一次。比如我们设定500ms,在这个时间内,无论点击按钮多少次,它都只会触发一次。具体场景可以是抢购时候,由于有无数人 快速点击按钮,如果每次点击都发送请求,就会给服务器造成巨大的压力,但是我们进行节流后,就会大大减少请求的次数。

  • 防抖
    防抖的意思是,在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效。具体场景可以搜索框输入关键字过程中实时 请求服务器匹配搜索结果,如果不进行处理,那么就是输入框内容一直变化,导致一直发送请求。如果进行防抖处理,结果就是当我们输入内容完成后,一定时间(比如500ms)没有再 输入内容,这时再触发请求。 

2、具体实现:

节流:

        throttle(func,wait = 500)

  • func <Function> 触发回调执行的函数
  • wait <Number> 时间间隔,单位ms
let timer_throttle = null
const throttle = (func, wait) => {
	if (!timer_throttle) {
		timer_throttle = setTimeout(() => {
			func.apply(this, arguments)
			timer_throttle = null;
		}, wait)
	}
}

防抖:

        debounce(func,wait = 500)

  • func <Function> 触发回调执行的函数
  • wait <Number> 时间间隔,单位ms
let timer_debounce = null
const debounce = function(func, wait) {
	if (timer_debounce) {
		clearTimeout(timer_debounce)
		timer_debounce = null;
	}
	timer_debounce = setTimeout(() => {
		func.apply(this, arguments)
	}, wait)
}

 3、使用:

可以在uilts文件夹下新建prototype.js文件:

//防抖
let timer_debounce = null
const debounce = function(func, wait) {
	if (timer_debounce) {
		clearTimeout(timer_debounce)
		timer_debounce = null;
	}
	timer_debounce = setTimeout(() => {
		func.apply(this, arguments)
	}, wait)
}

//节流
let timer_throttle = null
const throttle = function(func, wait) {
	if (!timer_throttle) {
		timer_throttle = setTimeout(() => {
			func.apply(this, arguments)
			timer_throttle = null;
		}, wait)
	}
}
export default {
	throttle,
	debounce
}

在main.js全局引入挂载:

import prototype from './uilts/prototype.js'
Vue.prototype.$prototype = prototype

在vue页面中使用:

let that = this
this.$prototype.debounce(function() {
	console.log('防抖', that.inputValue)
}, 500)
let that = this
this.$prototype.throttle(function() {
	console.log('节流', that.inputValue)
}, 500)

二、使用 Lodash 等组件库来实现:

Lodash 中文文档

安装组件库:

npm i --save lodash
或
yarn add lodash

引入挂载组件库:

import _ from "lodash";

节流、防抖函数:

_.throttle(func, [wait=0], [options=])//节流
//说明:
//func (Function): 要节流的函数。
//[wait=0] (number): 需要节流的毫秒。
//[options=] (Object): 选项对象。
//[options.leading=true] (boolean): 指定调用在节流开始前。
//[options.trailing=true] (boolean): 指定调用在节流结束后。

_.debounce(func, [wait=0], [options=])//防抖
//说明:
//func (Function): 要防抖动的函数。
//[wait=0] (number): 需要延迟的毫秒数。
//[options=] (Object): 选项对象。
//[options.leading=false] (boolean): 指定在延迟开始前调用。
//[options.maxWait] (number): 设置 func 允许被延迟的最大值。
//[options.trailing=true] (boolean): 指定在延迟结束后调用。

使用节流、防抖函数:

_.debounce(() => {
  console.log("防抖");
}, 500);
_.throttle(() => {
  console.log("节流");
}, 500);

Vue使用lodash来实现节流防抖的方法如下: 1. 安装lodash库:在项目根目录下打开命令行工具,执行以下命令安装lodash库: ``` npm install lodash ``` 2. 导入lodash库:在需要使用节流防抖的组件中,导入lodash库的相关函数: ```javascript import { debounce, throttle } from 'lodash'; ``` 3. 使用节流函数:使用`throttle`函数来实现节流。`throttle`函数会在指定的时间间隔内只执行一次函数,可以用于限制频繁触发的事件,例如滚动、拖拽等。以下是一个使用节流函数的示例: ```javascript export default { data() { return { scrollHandler: null }; }, methods: { handleScroll() { // 处理滚动事件的逻辑 console.log('Scroll event'); } }, mounted() { this.scrollHandler = throttle(this.handleScroll, 200); // 设置节流时间为200ms window.addEventListener('scroll', this.scrollHandler); }, beforeDestroy() { window.removeEventListener('scroll', this.scrollHandler); } } ``` 在上述代码中,使用`throttle`函数将`handleScroll`方法包装起来,设置节流时间为200ms。在组件的`mounted`生命周期钩子中,将节流后的处理函数添加到`scroll`事件监听器中,保证在指定时间间隔内只执行一次。 4. 使用防抖函数:使用`debounce`函数来实现防抖。`debounce`函数会在指定的时间间隔后执行函数,如果在这个时间间隔内又触发了同样的函数,则重新计时。以下是一个使用防抖函数的示例: ```javascript export default { data() { return { inputHandler: null, inputValue: '' }; }, methods: { handleInput() { // 处理输入事件的逻辑 console.log('Input event'); } }, mounted() { this.inputHandler = debounce(this.handleInput, 500); // 设置防抖时间为500ms } } ``` 在上述代码中,使用`debounce`函数将`handleInput`方法包装起来,设置防抖时间为500ms。在组件的`mounted`生命周期钩子中,将防抖后的处理函数赋值给`inputHandler`。 在模板中,将防抖处理函数绑定到相应的事件上: ```html <input type="text" v-model="inputValue" @input="inputHandler"> ``` 这样,在输入框输入内容时,只有在500ms内没有再次输入才会触发防抖后的处理函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值