vuejs-challenges
vuejs-challenges copied to clipboard
20 - 节流点击指令
// 你的答案
`
- 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 VDebounceClick = { mounted(el:HTMLButtonElement,binding:DirectiveBinding){ const {arg,value}=binding let timer el.addEventListener('click',()=>{ if(timer) return timer=setTimeout(()=>{ value && value() timer=null },+arg) }) } }
function onClick() { console.log("Only triggered once when clicked many times quickly") }
`