国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

淺談JS函數(shù)節(jié)流防抖

2024-05-06 15:25:31
字體:
供稿:網(wǎng)友

在前端開發(fā)中有一部分的用戶行為會頻繁的觸發(fā)事件執(zhí)行,而對于DOM操作、資源加載等耗費性能的處理,很可能導致界面卡頓,甚至瀏覽器的崩潰。函數(shù)節(jié)流(throttle)和函數(shù)防抖(debounce)就是為了解決類似需求應運而生的。

函數(shù)節(jié)流(throttle)

函數(shù)節(jié)流就是預定一個函數(shù)只有在大于等于執(zhí)行周期時才執(zhí)行,周期內(nèi)調(diào)用不執(zhí)行。好像水滴攢到一定重量才會落下一樣。

場景:

窗口調(diào)整(resize) 頁面滾動(scroll) 搶購瘋狂點擊(mousedown)

實現(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實現(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;    //計算剩余時間    var remaining = wait - (now - previous);    context = this;    args = arguments;    //剩余時間小于等于0或者剩余時間大于等待時間(本地時間變動出現(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ā)情況時,只有足夠空閑的時間,才執(zhí)行一次。好像公交司機會等人都上車后才出站一樣。

場景:

實時搜索(keyup) 拖拽(mousemove)

實現(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實現(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;  };};              
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 韶关市| 东山县| 突泉县| 陆河县| 铜鼓县| 麻江县| 达拉特旗| 进贤县| 桦川县| 吉木乃县| 靖江市| 五峰| 余姚市| 大洼县| 洪洞县| 龙游县| 青浦区| 江城| 都江堰市| 武定县| 金门县| 长春市| 阜康市| 壤塘县| 松江区| 轮台县| 南阳市| 大足县| 海原县| 清丰县| 温宿县| 奎屯市| 蓬溪县| 永丰县| 茂名市| 定州市| 越西县| 文安县| 光山县| 聊城市| 讷河市|