防抖函数的实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>防抖</title> </head> <body> <button id="btn">提交</button> <script> /*防抖函数作用限制多长时间内只能执行一次*/ function success (event){ console.log('提交成功') } //防抖函数 const debounce = (fn,delay) =>{ // 1 let timer = null return (...args) => { //4 clearTimeout(timer) //5 timer= setTimeout(() =>{//3 fn.apply(this,args) //6 },delay) } } const oDebounce = debounce(success,5000) //2 let btn = document.getElementById('btn') btn.addEventListener('click',oDebounce) </script> </body> </html>