自定义防抖指令 v-debounce-click:1000

文章介绍了如何在Vue.js中实现一个名为v-debounce-click的自定义指令,该指令确保在短时间内多次点击时,只触发一次事件处理函数,并支持延迟时间选项。通过debounce和throttle函数来控制事件执行频率。
<script setup lang='ts'>
import {ref} from 'vue'

/**
 * Implement the custom directive
 * Make sure the `onClick` method only gets triggered once when clicked many times quickly
 * And you also need to support the debounce delay time option. e.g `v-debounce-click:ms`
 *
*/
const testRef = ref('1')
const VDebounceClick = {
		mounted:(el,binding)=>{
    let {arg:time,value:fn}= binding
    	el.addEventListener('click',debounce(fn,time))
    }
}

function debounce(fn,delay){
	let time = null
  return function(){
  	if(time) clearTimeout(time)
    time = setTimeout(()=>{
    	fn.apply(this,arguments)
    },delay)
  }
}
function throttle(fn,delay){
	let sign = true
  return function(){
  	if(!sign) return
    sign = false
    setTimeout(()=>{
    	fn.apply(this,arguments)
      sign = true
    },delay)
  
  }
}

function onClick() {    
    testRef.value=testRef.value +'1'
  console.log("Only triggered once when clicked many times quickly")
}

</script>

<template>
  <button v-debounce-click:1000="onClick">
    Click on it many times quickly
  </button>
  <div>{{testRef}}</div>
</template>

/** * @Description: * @Author: 毛旺旺 * @Date: 2025-01-13 08:43:28 * @LastEditTime: 2025-07-20 00:33:17 */ import { createSSRApp } from 'vue'; import App from './App.vue'; import store from './store/index.js'; // #ifdef H5 // 初始化sdk // Web/JS SDK 集成文档链接:https://www.volcengine.com/docs/6285/93209 var VUE_APP_NODE_id = ''; var VUE_APP_NODE_domain = ''; if (import.meta.env.VITE_ENV == 'production') { VUE_APP_NODE_id = 10000002; VUE_APP_NODE_domain = 'https://datafinder.cebbank.com'; } else { VUE_APP_NODE_id = 10000008; VUE_APP_NODE_domain = 'https://mobiletest.cebbank.com:19443/'; } window.collectEvent('init', { app_id: parseInt(VUE_APP_NODE_id), channel_domain: VUE_APP_NODE_domain, log: true, // true:开启日志,false:关闭日志361679 channel: 'cn', // 上报通道标识 enable_native: true, autotrack: true, }); window.collectEvent('start'); // 通知SDK设置完毕,可以真正开始发送事件了 // #endif export function createApp() { const app = createSSRApp(App); app.directive('debounce', { mounted(el, binding) { let timer = null; el.addEventListener('click', () => { if (timer) clearTimeout(timer); timer = setTimeout(() => { binding.value(); }, binding.arg || 500); // 默认500ms }); }, }); app.use(store); return { app, }; } <view class="content-middle-item" v-debounce="toNearbyBranches" :data-id="'outlets'" :data-name="'附近网点'" > 报错信息是[vite:vue] unknown directive {"type":7,"name":"debounce","rawName":"v-debounce","exp":{"type":4,"loc":{"start":{"line":1,"column":1,"offset":0},"end":{"line":1,"column":1,"offset":0},"source":""},"content":"bV","isStatic":false,"constType":0},"modifiers":[],"loc":{"start":{"column":29,"line":587,"offset":35168},"end":{"column":58,"line":587,"offset":35197},"source":"v-debounce=\"toNearbyBranches\""}}
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值