定义:事件在n秒后触发,不管在这个时间节点中执行多少次事件,只有最后一次事件是触发 对比场景:游戏中的回城 DNF中的抬*使用场景:- 上拉刷新,下来加载--scroll事件- resize- 文本的输入验证(连续输入文字后发送Ajax请求进行验证,验证一次就够了)
1-1 原生JavaScript-button实现防抖
<body> <!-- 什么叫函数防抖:事件n秒后触发,不管在这个时间段中执行多少次事件,该事件只针对 最后一次触发 --> <button id="btn">函数防抖</button> <script> var btn = document.getElementById("btn"); var timer; btn.onclick = function(){ if(timer){ clearTimeout(timer); } timer = setTimeout(()=>{ console.log("loading"); timer = null },500) } /* 3s */ </script></body>
1-2 input实现防抖
<body> <input type="text" id="input"> <script> var input = document.getElementById("input"); var timer; input.addEventListener("keyup",()=>{ if(timer){ clearTimeout(timer); } timer = setTimeout(()=>{ console.log(input.value) timer = null; },1000) }) </script></body>
1-3 封装防抖
<body> <input type="text" id="input"> <script> var input = document.getElementById("input"); var timer /* 封装防抖函数 */ function debounce(fn,delay=500){ if(timer){ clearTimeout(timer); } timer = setTimeout(fn,delay) } input.addEventListener("keyup",()=>{ debounce(()=>{ console.log(input.value); }) }) </script></body>
1-4 闭包封装防抖
<body> <input type="text" id="input"> <script> var input = document.getElementById("input"); input.addEventListener("keyup",debounce(()=>{ console.log(input.value); })) /* 闭包 1、返回值是一个函数 2、返回函数使用返回函数外面定义的局部变量 闭包好处:不会造成全局的污染 */ function debounce(fn,delay=500){ let timer = null; return function(){ if(timer){ clearTimeout(timer); } timer = setTimeout(fn,delay); } } </script></body>
1-5 apply
<body> <input type="text" id="input"> <script> var input = document.getElementById("input"); input.addEventListener("keyup",debounce(function(){ console.log(this.value) })) /* 闭包 1、返回值是一个函数 2、返回函数使用返回函数外面定义的局部变量 闭包好处:不会造成全局的污染 */ function debounce(fn,delay=500){ let timer = null; return function(){ console.log(this) if(timer){ clearTimeout(timer); } timer = setTimeout(()=>{ console.log(this) fn.call(this); },delay); } } /* function才能使用call,apply */ </script></body>