防抖
防抖就是公交车等人上车,有人上等5s,5s内又有人上再等5s --动态
节流就是公交车固定15分钟一班车 --静态
export const directives = (app:any) => { //vue防抖 app.directive('debounce', { mounted(el:any,binding:any) { if(typeof binding.value !=='function') return el.timer = null el.handler = function () { if (el.timer) { clearTimeout(el.timer) } el.timer = setTimeout(() => { binding.value.apply(this, arguments) },600) } el.addEventListener('click',el.handler) }, beforeUnmount(el:any, binding:any) { if(el.timer){ el.timer = null clearTimeout(el.timer) } el.removeEventListener('click',el.handler) }, }) }
//main.ts全局注册指令 import {directives} from './utils/common' const app = createApp(App); directives(app)
//使用 <button class="confirm-btn" v-if="state.item.is_contract_type===2" v-debounce="confirmChange">确认签约</button>