函数防抖 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>

  

posted @ 2017-10-12 14:21  风吹麦浪打  阅读(386)  评论(0编辑  收藏  举报