函数防抖 debounce
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <button class="btn">点击</button> <script src="jquery.min.js"></script> <script> //按钮点击出发防抖动的函数 debounce $(".btn").on("click",debounce(Click,1000)) function debounce (fn, duration){ //fn(你要防止抖动的函数) duration:(毫秒数) var timer = null; return function (){ clearTimeout(timer) timer=setTimeout(function(){ fn() },duration) } }
//你要运行函数 function Click(){ console.log(1) //todo something } </script> </body> </html>