第四天:防抖
<h4>1.防抖</h4>
<blockquote>
<p>触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次触发,则重新计算时间</p>
</blockquote>
<h4>思路:每次触发事件时都取消之前的延时调用方法</h4>
<pre><code>function debounce(fn,delay){
let timeout = null;//创建一个标记用来存放定时器的返回值
return function(){
clearTimeout(timeout); // 每当用户输入的时候把前一个 setTimeout clear 掉
timeout = setTimeout(() => {
fn.apply(this, arguments);
},delay)
};
}
// 然后又创建一个新的 setTimeout, 这样就能保证输入字符后的 interval 间隔内如果还有字符输入的话,就不会执行 fn 函数
function sayHi() {
console.log('防抖成功')
}</code></pre>
<h4>2.节流</h4>
<blockquote>
<p>高频事件触发,但在n秒内只会执行一次,所以节流会稀释函数的执行频率</p>
<h4>思路:每次触发时间前都会判断当前是否有等待执行的延时函数</h4>
</blockquote>
<pre><code>function throttle(fn) {
let canRun = true; // 通过闭包保存一个标记
return function () {
if (!canRun) return; // 在函数开头判断标记是否为true,不为true则return
canRun = false; // 立即设置为false
setTimeout(() => { // 将外部传入的函数的执行放在setTimeout中
fn.apply(this, arguments);
// 最后在setTimeout执行完毕后再把标记设置为true(关键)表示可以执行下一次循环了。当定时器没有执行的时候标记永远是false,在开头被return掉
canRun = true;
}, 500);
};
}
function sayHi(e) {
console.log(e.target.innerWidth, e.target.innerHeight);
}
window.addEventListener('resize', throttle(sayHi));</code></pre>