最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何解決js函數(shù)防抖、節(jié)流出現(xiàn)的問題

 更新時(shí)間:2019年06月17日 11:06:29   作者:全棧弄潮兒  
這篇文章主要介紹了如何解決js函數(shù)防抖、節(jié)流出現(xiàn)的問題。SyntheticEvent對(duì)象是通過合并得到的。 這意味著在事件回調(diào)被調(diào)用后,SyntheticEvent 對(duì)象將被重用并且所有屬性都將被取消。 因此,您無法以異步方式訪問該事件。,需要的朋友可以參考下

React中使用防抖函數(shù)和節(jié)流函數(shù)

在React事件調(diào)用時(shí),React傳遞給事件處理程序是一個(gè)合成事件對(duì)象的實(shí)例。SyntheticEvent對(duì)象是通過合并得到的。 這意味著在事件回調(diào)被調(diào)用后,SyntheticEvent 對(duì)象將被重用并且所有屬性都將被取消。 這是出于性能原因。 因此,您無法以異步方式訪問該事件。React合成事件官方文檔

所以在用防抖或節(jié)流函數(shù)封裝時(shí),異步方式訪問事件對(duì)象出現(xiàn)問題。解決的方法如下:

方法一:調(diào)用合成事件對(duì)象的persist()方法 event.persist && event.persist() //保留對(duì)事件的引用

方法二:深拷貝事件對(duì)象 const event = e && {...e} //深拷貝事件對(duì)象

function debounce(func, wait=500) {
let timeout; // 定時(shí)器變量
return function(event){
clearTimeout(timeout); // 每次觸發(fā)時(shí)先清除上一次的定時(shí)器,然后重新計(jì)時(shí)
event.persist && event.persist() //保留對(duì)事件的引用
//const event = e && {...e} //深拷貝事件對(duì)象
timeout = setTimeout(()=>{
func(event)
}, wait); // 指定 xx ms 后觸發(fā)真正想進(jìn)行的操作 handler
};
}

防抖debounce

防抖 Debounce 多次觸發(fā),只在最后一次觸發(fā)時(shí),執(zhí)行目標(biāo)函數(shù)。

函數(shù)防抖就是,延遲一段時(shí)間再執(zhí)行函數(shù),如果這段時(shí)間內(nèi)又觸發(fā)了該函數(shù),則延遲重新計(jì)算。

應(yīng)用場景

(1)通過監(jiān)聽某些事件完成對(duì)應(yīng)的需求,比如:

通過監(jiān)聽 scroll 事件,檢測滾動(dòng)位置,根據(jù)滾動(dòng)位置顯示返回頂部按鈕

通過監(jiān)聽 resize 事件,對(duì)某些自適應(yīng)頁面調(diào)整DOM的渲染(通過CSS實(shí)現(xiàn)的自適應(yīng)不再此范圍內(nèi))

通過監(jiān)聽 keyup 事件,監(jiān)聽文字輸入并調(diào)用接口進(jìn)行模糊匹配

(2)其他場景

表單組件輸入內(nèi)容驗(yàn)證

防止多次點(diǎn)擊導(dǎo)致表單多次提交

簡單實(shí)現(xiàn)

function debounce(fn, wait) {
let t
return () => {
let context = this
let args = arguments
if (t) clearTimeout(t)
t= setTimeout(() => {
fn.apply(context, args)
}, wait)
}
}

完整實(shí)現(xiàn)

function debounce(func, wait, immediate) {
let time;
let debounced = function() {
let context = this;
if(time) clearTimeout(time);
if(immediate) {
let callNow = !time;
if(callNow) func.apply(context, arguments);
time = setTimeout(
()=>{time = null} //見注解
, wait)
} else {
time = setTimeout(
()=>{func.apply(context, arguments)}
, wait) 
}
};
debounced.cancel = function() {
clearTimeout(time);
time = null
};
return debounced
}
// underscore.js debounce
//
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
// 處理時(shí)間
var later = function() {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last); // 10ms 6ms 4ms
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};

react中調(diào)用方法

this.handleGetCustomerNameList = debounce(this.handleGetCustomerNameList.bind(this), 500);

節(jié)流 throttle

節(jié)流:函數(shù)間隔一段時(shí)間后才能再觸發(fā),避免某些函數(shù)觸發(fā)頻率過高,比如滾動(dòng)條滾動(dòng)事件觸發(fā)的函數(shù)。

### 簡單實(shí)現(xiàn)
function throttle (fn, wait, mustRun) {
let start = new Date()
let timeout
return () => {
// 在返回的函數(shù)內(nèi)部保留上下文和參數(shù)
let context = this
let args = arguments
let current = new Date()
clearTimeout(timeout)
let remaining = current - start
// 達(dá)到了指定觸發(fā)時(shí)間,觸發(fā)該函數(shù)
if (remaining > mustRun) {
fn.apply(context, args)
start = current
} else {
// 否則wait時(shí)間后觸發(fā),閉包保留一個(gè)timeout實(shí)例
timeout = setTimeout(fn, wait);
}
}
}

完整實(shí)現(xiàn)

function throttle(func, wait, options) {
let time, context, args, result;
let previous = 0;
if (!options) options = {};
let later = function () {
previous = options.leading === false ? 0 : new Date().getTime();
time = null;
func.apply(context, args);
if (!time) context = args = null;
};
let throttled = function () {
let now = new Date().getTime();
if (!previous && options.leading === false) previous = now;
let remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (time) {
clearTimeout(time);
time = null;
}
previous = now;
func.apply(context, args);
if (!time) context = args = null;
} else if (!time && options.trailing !== false) {
time = setTimeout(later, remaining);
}
};
return throttled;
}
// underscore.js throttle
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.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;
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;
};
};

react中調(diào)用方法

this.handleGetCustomerNameList = throttle (this.handleGetCustomerNameList.bind(this), 500);

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaScript生成簡單等差數(shù)列

    JavaScript生成簡單等差數(shù)列

    本文給大家分享使用for循環(huán)實(shí)現(xiàn)js生成簡單的等差數(shù)列,具體實(shí)現(xiàn)方法,大家參考下本文
    2017-11-11
  • 如何在JavaScript中使用msgpack-lite 和zlib實(shí)現(xiàn)大數(shù)據(jù)文件的壓縮和讀取

    如何在JavaScript中使用msgpack-lite 和zlib實(shí)現(xiàn)大數(shù)據(jù)文件的壓縮和讀取

    本文介紹msgpack-lite和zlib在JavaScript中的應(yīng)用,結(jié)合實(shí)例代碼講解如何在JavaScript中使用msgpack-lite 和zlib實(shí)現(xiàn)大數(shù)據(jù)文件的壓縮和讀取,感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • 使用plupload自定義參數(shù)實(shí)現(xiàn)多文件上傳

    使用plupload自定義參數(shù)實(shí)現(xiàn)多文件上傳

    這篇文章主要介紹了使用plupload自定義參數(shù)實(shí)現(xiàn)多文件上傳的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • JavaScript強(qiáng)制類型轉(zhuǎn)換和隱式類型轉(zhuǎn)換操作示例

    JavaScript強(qiáng)制類型轉(zhuǎn)換和隱式類型轉(zhuǎn)換操作示例

    這篇文章主要介紹了JavaScript強(qiáng)制類型轉(zhuǎn)換和隱式類型轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了javascript字符串、數(shù)字等顯示類型轉(zhuǎn)換,以及運(yùn)算、判斷等情況下的隱式類型轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2019-05-05
  • js簡單判斷移動(dòng)端系統(tǒng)的方法

    js簡單判斷移動(dòng)端系統(tǒng)的方法

    這篇文章主要介紹了js簡單判斷移動(dòng)端系統(tǒng)的方法,通過JavaScript的navigator.userAgent相關(guān)屬性判斷訪問端的系統(tǒng)類型,非常簡單實(shí)用,需要的朋友可以參考下
    2016-02-02
  • js 遞歸和定時(shí)器的實(shí)例解析

    js 遞歸和定時(shí)器的實(shí)例解析

    本文主要介紹了js遞歸和定時(shí)器的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 微信小程序-詳解微信登陸、微信支付、模板消息

    微信小程序-詳解微信登陸、微信支付、模板消息

    本篇文章主要是介紹了微信小程序-微信登陸、微信支付、模板消息,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • JS處理下劃線的方法步驟

    JS處理下劃線的方法步驟

    本文主要介紹了使用JavaScript處理下劃線,特別是在雙引號(hào)內(nèi)的下劃線不作為分隔符的情況,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • JavaScript實(shí)現(xiàn)深拷貝的不同方法匯總

    JavaScript實(shí)現(xiàn)深拷貝的不同方法匯總

    在JavaScript中,我們經(jīng)常需要將一個(gè)對(duì)象的所有數(shù)據(jù)復(fù)制到另一個(gè)對(duì)象中,這種操作可以分為 淺拷貝 和 深拷貝,而深拷貝則是指完全復(fù)制一個(gè)對(duì)象及其所有嵌套的對(duì)象,而不僅僅是對(duì)象的引用,本文,我們將深入探討深拷貝的概念,以及如何使用不同的方法實(shí)現(xiàn)深拷貝
    2025-06-06
  • js判斷背景圖片是否加載成功使用img的width實(shí)現(xiàn)

    js判斷背景圖片是否加載成功使用img的width實(shí)現(xiàn)

    判斷背景圖片是否加載成功想必大家對(duì)此很陌生吧,會(huì)了之后就可以判斷css背景圖片了,具體判斷代碼如下,感興趣的朋友可以參考下哈
    2013-05-05

最新評(píng)論

馆陶县| 泸水县| 贡嘎县| 久治县| 洪湖市| 安塞县| 屯留县| 莱阳市| 普兰县| 探索| 大洼县| 东至县| 烟台市| 福海县| 华阴市| 平舆县| 松滋市| 万荣县| 开鲁县| 苍南县| 通化县| 琼结县| 阳山县| 潍坊市| 行唐县| 阿拉尔市| 洞口县| 凤庆县| 句容市| 洪江市| 多伦县| 长沙市| 武邑县| 德格县| 满洲里市| 汉源县| 镶黄旗| 惠安县| 南木林县| 云安县| 富宁县|