在前端開發(fā)的過程中比藻,我們經(jīng)常會需要綁定一些持續(xù)觸發(fā)的事件边琉,如 resize、scroll秃症、mousemove 等等候址,但有些時候我們并不希望在事件持續(xù)觸發(fā)的過程中那么頻繁地去執(zhí)行函數(shù)。
通常這種情況下我們怎么去解決的呢种柑?一般來講岗仑,防抖和節(jié)流是比較好的解決方案。
讓我們先來看看在事件持續(xù)觸發(fā)的過程中頻繁執(zhí)行函數(shù)是怎樣的一種情況聚请。
html 文件中代碼如下
<div id="content" style="height:150px;line-height:150px;text-align:center; color: #fff;background-color:#ccc;font-size:80px;"></div>
<script>
let num = 1;
let content = document.getElementById('content');
function count() {
content.innerHTML = num++;
};
content.onmousemove = count;
</script>
在上述代碼中荠雕,div 元素綁定了 mousemove 事件,當(dāng)鼠標(biāo)在 div(灰色)區(qū)域中移動的時候會持續(xù)地去觸發(fā)該事件導(dǎo)致頻繁執(zhí)行函數(shù)驶赏。效果如下
可以看到炸卑,在沒有通過其它操作的情況下,函數(shù)被頻繁地執(zhí)行導(dǎo)致頁面上數(shù)據(jù)變化特別快母市。所以矾兜,接下來讓我們來看看防抖和節(jié)流是如何去解決這個問題的。
防抖(debounce)
所謂防抖患久,就是指觸發(fā)事件后在 n 秒內(nèi)函數(shù)只能執(zhí)行一次椅寺,如果在 n 秒內(nèi)又觸發(fā)了事件,則會重新計(jì)算函數(shù)執(zhí)行時間蒋失。
防抖函數(shù)分為非立即執(zhí)行版和立即執(zhí)行版返帕。
非立即執(zhí)行版:
function debounce(func, wait) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args)
}, wait);
}
}
非立即執(zhí)行版的意思是觸發(fā)事件后函數(shù)不會立即執(zhí)行,而是在 n 秒后執(zhí)行篙挽,如果在 n 秒內(nèi)又觸發(fā)了事件荆萤,則會重新計(jì)算函數(shù)執(zhí)行時間。
我們依舊使用上述綁定 mousemove 事件的例子,通過上面的防抖函數(shù)链韭,我們可以這么使用
content.onmousemove = debounce(count,1000);
效果如下
可以看到偏竟,在觸發(fā)事件后函數(shù) 1 秒后才執(zhí)行,而如果我在觸發(fā)事件后的 1 秒內(nèi)又觸發(fā)了事件敞峭,則會重新計(jì)算函數(shù)執(zhí)行時間踊谋。
上述防抖函數(shù)的代碼還需要注意的是 this 和 參數(shù)的傳遞
let context = this;
let args = arguments;
防抖函數(shù)的代碼使用這兩行代碼來獲取 this 和 參數(shù),是為了讓 debounce 函數(shù)最終返回的函數(shù) this 指向不變以及依舊能接受到 e 參數(shù)旋讹。
立即執(zhí)行版:
function debounce(func,wait) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
let callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
}
}
立即執(zhí)行版的意思是觸發(fā)事件后函數(shù)會立即執(zhí)行殖蚕,然后 n 秒內(nèi)不觸發(fā)事件才能繼續(xù)執(zhí)行函數(shù)的效果。
使用方法同上沉迹,效果如下
在開發(fā)過程中睦疫,我們需要根據(jù)不同的場景來決定我們需要使用哪一個版本的防抖函數(shù),一般來講上述的防抖函數(shù)都能滿足大部分的場景需求鞭呕。但我們也可以將非立即執(zhí)行版和立即執(zhí)行版的防抖函數(shù)結(jié)合起來蛤育,實(shí)現(xiàn)最終的雙劍合璧版的防抖函數(shù)。
雙劍合璧版:
/**
* @desc 函數(shù)防抖
* @param func 函數(shù)
* @param wait 延遲執(zhí)行毫秒數(shù)
* @param immediate true 表立即執(zhí)行葫松,false 表非立即執(zhí)行
*/
function debounce(func,wait,immediate) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
}
else {
timeout = setTimeout(function(){
func.apply(context, args)
}, wait);
}
}
}
節(jié)流(throttle)
所謂節(jié)流缨伊,就是指連續(xù)觸發(fā)事件但是在 n 秒中只執(zhí)行一次函數(shù)。節(jié)流會稀釋函數(shù)的執(zhí)行頻率进宝。
對于節(jié)流刻坊,一般有兩種方式可以實(shí)現(xiàn),分別是時間戳版和定時器版党晋。
時間戳版:
function throttle(func, wait) {
let previous = 0;
return function() {
let now = Date.now();
let context = this;
let args = arguments;
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}
}
使用方式如下
content.onmousemove = throttle(count,1000);
效果如下
可以看到谭胚,在持續(xù)觸發(fā)事件的過程中,函數(shù)會立即執(zhí)行未玻,并且每 1s 執(zhí)行一次灾而。
定時器版:
function throttle(func, wait) {
let timeout;
return function() {
let context = this;
let args = arguments;
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
使用方式同上,效果如下
可以看到扳剿,在持續(xù)觸發(fā)事件的過程中旁趟,函數(shù)不會立即執(zhí)行,并且每 1s 執(zhí)行一次庇绽,在停止觸發(fā)事件后锡搜,函數(shù)還會再執(zhí)行一次。
我們應(yīng)該可以很容易的發(fā)現(xiàn)瞧掺,其實(shí)時間戳版和定時器版的節(jié)流函數(shù)的區(qū)別就是耕餐,時間戳版的函數(shù)觸發(fā)是在時間段內(nèi)開始的時候,而定時器版的函數(shù)觸發(fā)是在時間段內(nèi)結(jié)束的時候辟狈。
同樣地肠缔,我們也可以將時間戳版和定時器版的節(jié)流函數(shù)結(jié)合起來夏跷,實(shí)現(xiàn)雙劍合璧版的節(jié)流函數(shù)。
雙劍合璧版:
/**
* @desc 函數(shù)節(jié)流
* @param func 函數(shù)
* @param wait 延遲執(zhí)行毫秒數(shù)
* @param type 1 表時間戳版明未,2 表定時器版
*/
function throttle(func, wait ,type) {
if(type===1){
let previous = 0;
}else if(type===2){
let timeout;
}
return function() {
let context = this;
let args = arguments;
if(type===1){
let now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}else if(type===2){
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}