vue3+ts通过lodash实现防抖节流

安装lodash

npm i --save-dev @types/lodash

在组件中引入lodash

import * as _ from 'lodash'

防抖

_.debounce(func, [wait=0], [options=]) 函数在延迟几毫秒之后才执行,也就是停止改变几秒后执行

参数

  1. func (Function): 要防抖动的函数。
  2. [wait=0] (number): 需要延迟的毫秒数。
  3. [options=] (Object): 选项对象。
  4. [options.leading=false] (boolean): 指定在延迟开始前调用。
  5. [options.maxWait] (number): 设置 func 允许被延迟的最大值。
  6. [options.trailing=true] (boolean): 指定在延迟结束后调用。
<script setup lang="ts">
	import * as _ from 'lodash'

	//防抖的函数应该是click事件
	const fangdou = _.debounce(click, 500, {
	  leading: true,  // 延长开始后调用
	  trailing: false  // 延长结束前调用
	})
	
	function click() {
		//响应点击
	  console.log("123")
	}
	//移除组件时,取消防抖
	onUnmounted(()=>{
	  fangdou.cancel()
	}) 

</script>

<template>
  <button @click="fangdou">fangdou</button>
</template>

节流

_.throttle(func, [wait=0], [options=]) 第一次会立即执行一次,然后等到过了毫秒数才会执行,以一定的频率后续处理

参数

  1. func (Function): 要节流的函数。
  2. [wait=0] (number): 需要节流的毫秒。
  3. [options=] (Object): 选项对象。
  4. [options.leading=true] (boolean): 指定调用在节流开始前。
  5. [options.trailing=true] (boolean): 指定调用在节流结束后。
<script setup lang="ts">
   import * as _ from 'lodash'

   const throttle = _.throttle(() =>{
     console.log('I get fired every two seconds!')
   },2000,{
     leading: true,
     trailing: false
   })
   //移除组件时,取消节流
   onUnmounted(()=>{
     throttle.cancel()
   }) 
</script>

<template>
   <button @click="throttle">jieliu</button>
</template>
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中结合TypeScript封装全局的防抖节流函数可以通过创建一个`utils`文件,并在Vue应用的入口文件中进行全局注册。下面是一个示例: ```typescript // utils/debounce.ts export function debounce(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; return function(...args: any[]) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { func.apply(this, args); }, delay); }; } // utils/throttle.ts export function throttle(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; let lastRun = 0; return function(...args: any[]) { const currentTime = Date.now(); if (timer) { clearTimeout(timer); } if (currentTime - lastRun >= delay) { func.apply(this, args); lastRun = currentTime; } else { timer = setTimeout(() => { func.apply(this, args); lastRun = Date.now(); }, delay); } }; } ``` 然后,在Vue应用的入口文件(如`main.ts`)中,可以将这些函数注册为全局方法: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import { debounce, throttle } from './utils'; const app = createApp(App); app.config.globalProperties.$debounce = debounce; app.config.globalProperties.$throttle = throttle; app.mount('#app'); ``` 现在,你可以在Vue组件中使用`this.$debounce`和`this.$throttle`来调用防抖节流函数了。 ```vue <template> <div> <input type="text" @input="handleInput" /> </div> </template> <script> export default { methods: { handleInput: this.$debounce(function(event) { console.log(event.target.value); }, 300), }, }; </script> ``` 在上面的示例中,`handleInput`方法使用防抖函数,延迟300毫秒后执行。你可以根据实际需求调整防抖节流的延迟时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值