<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        如何解決js函數防抖、節流出現的問題

        來源:懂視網 責編:小采 時間:2020-11-27 21:55:02
        文檔

        如何解決js函數防抖、節流出現的問題

        如何解決js函數防抖、節流出現的問題:React中使用防抖函數和節流函數 在React事件調用時,React傳遞給事件處理程序是一個合成事件對象的實例。SyntheticEvent對象是通過合并得到的。 這意味著在事件回調被調用后,SyntheticEvent 對象將被重用并且所有屬性都將被取消。 這是出于性能原因。 因
        推薦度:
        導讀如何解決js函數防抖、節流出現的問題:React中使用防抖函數和節流函數 在React事件調用時,React傳遞給事件處理程序是一個合成事件對象的實例。SyntheticEvent對象是通過合并得到的。 這意味著在事件回調被調用后,SyntheticEvent 對象將被重用并且所有屬性都將被取消。 這是出于性能原因。 因

        React中使用防抖函數和節流函數

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

        所以在用防抖或節流函數封裝時,異步方式訪問事件對象出現問題。解決的方法如下:

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

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

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

        防抖debounce

        防抖 Debounce 多次觸發,只在最后一次觸發時,執行目標函數。

        函數防抖就是,延遲一段時間再執行函數,如果這段時間內又觸發了該函數,則延遲重新計算。

        應用場景

        (1)通過監聽某些事件完成對應的需求,比如:

        通過監聽 scroll 事件,檢測滾動位置,根據滾動位置顯示返回頂部按鈕

        通過監聽 resize 事件,對某些自適應頁面調整DOM的渲染(通過CSS實現的自適應不再此范圍內)

        通過監聽 keyup 事件,監聽文字輸入并調用接口進行模糊匹配

        (2)其他場景

        表單組件輸入內容驗證

        防止多次點擊導致表單多次提交

        簡單實現

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

        完整實現

        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;
        // 處理時間
        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中調用方法

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

        節流 throttle

        節流:函數間隔一段時間后才能再觸發,避免某些函數觸發頻率過高,比如滾動條滾動事件觸發的函數。

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

        完整實現

        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中調用方法

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

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        如何解決js函數防抖、節流出現的問題

        如何解決js函數防抖、節流出現的問題:React中使用防抖函數和節流函數 在React事件調用時,React傳遞給事件處理程序是一個合成事件對象的實例。SyntheticEvent對象是通過合并得到的。 這意味著在事件回調被調用后,SyntheticEvent 對象將被重用并且所有屬性都將被取消。 這是出于性能原因。 因
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产AV无码专区亚洲AVJULIA | 亚洲婷婷国产精品电影人久久| 亚洲人成免费电影| 最近中文字幕无免费| 亚洲黄色网址在线观看| 一级毛片免费观看不卡视频| 亚洲成AV人在线播放无码 | 亚洲Av永久无码精品三区在线| 久久av免费天堂小草播放| 国产性爱在线观看亚洲黄色一级片 | 午夜国产大片免费观看| 国产成人亚洲精品无码AV大片| 免费又黄又爽的视频| 永久免费精品影视网站| 久久亚洲欧洲国产综合| 东北美女野外bbwbbw免费| 亚洲天堂视频在线观看| 91免费国产在线观看| 亚洲国产成人精品无码区花野真一 | 久久久亚洲精华液精华液精华液| 波多野结衣久久高清免费| 一级白嫩美女毛片免费| 亚洲乱色熟女一区二区三区丝袜| 日韩内射激情视频在线播放免费| 91嫩草私人成人亚洲影院| 成年女人毛片免费播放视频m| 国产精品亚洲五月天高清| 中文字幕精品亚洲无线码一区| 久久99热精品免费观看牛牛| 亚洲天堂一区二区三区四区| 成人一a毛片免费视频| 一个人免费播放在线视频看片| 亚洲AV无码一区东京热久久| 成人免费一区二区无码视频| 一道本在线免费视频| 国产A在亚洲线播放| 午夜免费福利在线| 十八禁在线观看视频播放免费| 久久精品国产亚洲AV久| 亚洲精品一级无码鲁丝片| 亚洲成人在线免费观看|