函數(shù)防抖和函數(shù)節(jié)流
函數(shù)防抖和函數(shù)節(jié)流:是優(yōu)化高頻率執(zhí)行的JavaScript代碼的一種手段伶跷。常用于JavaScript中的一些事件壳炎,如瀏覽器的resize那婉、scroll眯娱,鼠標(biāo)的mousemove斑鸦、mouseover胖烛,鍵盤的keypress等临扮。例如input輸入框的keypress事件在觸發(fā)時毡咏,會不斷地調(diào)用綁定在事件上的回調(diào)函數(shù)陋率,極大地浪費(fèi)資源球化,降低前端性能。為了優(yōu)化體驗瓦糟,就需要對這類事件進(jìn)行調(diào)用次數(shù)的限制筒愚。
函數(shù)防抖(debounce)
所謂防抖,就是指觸發(fā)事件后在 n 秒內(nèi)函數(shù)只能執(zhí)行一次菩浙,如果在 n 秒內(nèi)又觸發(fā)了事件巢掺,則會重新計算函數(shù)執(zhí)行時間。防抖函數(shù)分為非立即執(zhí)行版和立即執(zhí)行版:
非立即執(zhí)行版:
//非立即執(zhí)行版的意思是觸發(fā)事件后函數(shù)不會立即執(zhí)行劲蜻,而是在 n 秒后執(zhí)行陆淀,如果在 n 秒內(nèi)又觸發(fā)了事件,則會重新計算函數(shù)執(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í)行版:
//立即執(zhí)行版的意思是觸發(fā)事件后函數(shù)會立即執(zhí)行轧苫,然后 n 秒內(nèi)不觸發(fā)事件才能繼續(xù)執(zhí)行函數(shù)的效果。
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)
}
}
封裝版本:
/**
* @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);
}
}
}
函數(shù)節(jié)流(throttle)
所謂節(jié)流浸剩,就是指連續(xù)觸發(fā)事件但是在 n 秒中只執(zhí)行一次函數(shù)。節(jié)流會稀釋函數(shù)的執(zhí)行頻率鳄袍。
對于節(jié)流绢要,一般有兩種方式可以實現(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;
}
}
}
定時器版:
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)
}
}
}
時間戳版和定時器版的節(jié)流函數(shù)的區(qū)別就是重罪,時間戳版的函數(shù)觸發(fā)是在時間段內(nèi)開始的時候,而定時器版的函數(shù)觸發(fā)是在時間段內(nèi)結(jié)束的時候哀九。
封裝版本:
/**
* @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)
}
}
}
}
案例
div 元素綁定了 mousemove 事件,當(dāng)鼠標(biāo)在 div(灰色)區(qū)域中移動的時候會持續(xù)地去觸發(fā)該事件導(dǎo)致頻繁執(zhí)行函數(shù)阅束。
<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 = debounce(count,1000,true );
//節(jié)流
content.onmousemove = throttle(count,1000,1 );
</script>