淺談JS函數(shù)節(jié)流防抖
在前端開發(fā)中有一部分的用戶行為會(huì)頻繁的觸發(fā)事件執(zhí)行,而對(duì)于DOM操作、資源加載等耗費(fèi)性能的處理,很可能導(dǎo)致界面卡頓,甚至瀏覽器的崩潰。函數(shù)節(jié)流(throttle)和函數(shù)防抖(debounce)就是為了解決類似需求應(yīng)運(yùn)而生的。
函數(shù)節(jié)流(throttle)
函數(shù)節(jié)流就是預(yù)定一個(gè)函數(shù)只有在大于等于執(zhí)行周期時(shí)才執(zhí)行,周期內(nèi)調(diào)用不執(zhí)行。好像水滴攢到一定重量才會(huì)落下一樣。
場(chǎng)景:
- 窗口調(diào)整(resize)
- 頁(yè)面滾動(dòng)(scroll)
- 搶購(gòu)瘋狂點(diǎn)擊(mousedown)
實(shí)現(xiàn):
function throttle(method, delay){
var last = 0;
return function (){
var now = +new Date();
if(now - last > delay){
method.apply(this,arguments);
last = now;
}
}
}
document.getElementById('throttle').onclick = throttle(function(){console.log('click')},2000);
underscore實(shí)現(xiàn):
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
//計(jì)算剩余時(shí)間
var remaining = wait - (now - previous);
context = this;
args = arguments;
//剩余時(shí)間小于等于0或者剩余時(shí)間大于等待時(shí)間(本地時(shí)間變動(dòng)出現(xiàn))
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
函數(shù)防抖(debounce)
函數(shù)防抖就是在函數(shù)需要頻繁觸發(fā)情況時(shí),只有足夠空閑的時(shí)間,才執(zhí)行一次。好像公交司機(jī)會(huì)等人都上車后才出站一樣。
場(chǎng)景:
- 實(shí)時(shí)搜索(keyup)
- 拖拽(mousemove)
實(shí)現(xiàn):
function debounce(method, delay){
var timer = null;
return function(){
var context = this,args = arguments;
clearTimeout(timer);
timer = setTimeout(function(){
method.apply(context, args);
},delay);
}
}
document.getElementById('debounce').onclick = debounce(function(){console.log('click')},2000);
underscore實(shí)現(xiàn):
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
函數(shù)節(jié)流(throttle)和函數(shù)防抖(debounce)都是通過延時(shí)邏輯操作來提升性能的方法,在前端優(yōu)化中是常見且重要的解決方式??梢詮母拍詈蛯?shí)際應(yīng)用中理解兩者的區(qū)別,在需要的時(shí)候選擇合適的方法處理。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript函數(shù)節(jié)流和函數(shù)防抖之間的區(qū)別
- JS函數(shù)節(jié)流和函數(shù)防抖問題分析
- 如何解決js函數(shù)防抖、節(jié)流出現(xiàn)的問題
- JS函數(shù)節(jié)流和防抖之間的區(qū)分和實(shí)現(xiàn)詳解
- js防抖函數(shù)和節(jié)流函數(shù)使用場(chǎng)景和實(shí)現(xiàn)區(qū)別示例分析
- javascript函數(shù)的節(jié)流[throttle]與防抖[debounce]
- 如何在面試中手寫出javascript節(jié)流和防抖函數(shù)
- Javascript節(jié)流函數(shù)throttle和防抖函數(shù)debounce
- 淺談JavaScript節(jié)流和防抖函數(shù)
- JS防抖節(jié)流函數(shù)的實(shí)現(xiàn)與使用場(chǎng)景
相關(guān)文章
javascript日期對(duì)象格式化為字符串的實(shí)現(xiàn)方法
本篇文章主要是對(duì)javascript日期對(duì)象格式化為字符串的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
JavaScript Canvas實(shí)現(xiàn)驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了JavaScript Canvas實(shí)現(xiàn)驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
原生JS實(shí)現(xiàn)勻速圖片輪播動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)勻速圖片輪播動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
實(shí)現(xiàn)JavaScript中數(shù)據(jù)響應(yīng)的方法總結(jié)
JavaScript 數(shù)據(jù)響應(yīng)是一種重要的前端開發(fā)概念,是指在應(yīng)用程序中的數(shù)據(jù)發(fā)生變化時(shí),能夠自動(dòng)更新與這些數(shù)據(jù)相關(guān)的用戶界面(UI)部分的能力,本文我們來總結(jié)一下目前可以簡(jiǎn)單實(shí)現(xiàn) JavaScript 中的數(shù)據(jù)響應(yīng)的方法,需要的朋友可以參考下2023-09-09

