zoukankan      html  css  js  c++  java
  • Other

    原文地址 www.jianshu.com

    节流与防抖的区别
    节流与防抖的前提都是某个行为持续地触发,不同之处只要判断是要优化到减少它的执行次数还是只执行一次就行。

    • 节流例子,像 dom 的拖拽,如果用消抖的话,就会出现卡顿的感觉,因为只在停止的时候执行了一次,这个时候就应该用节流,在一定时间内多次执行,会流畅很多。

    • 防抖例子,像仿百度搜索,就应该用防抖,当我连续不断输入时,不会发送请求;当我一段时间内不输入了,才会发送一次请求;如果小于这段时间继续输入的话,时间会重新计算,也不会发送请求。

    注释:节流是减少频率,防抖是只执行一次。

    debounce(防抖)

    防抖分为立即防抖非立即防抖
    最常见的例子就是:搜索

    非立即防抖

    注释:等待 wait 时间后才执行函数,在等待时如果再一次触发,会重置 wait 时间

    function debounce(func, wait) {
        var timeout = null;
        var context = this;
        var args = arguments;
        return function () {
            if (timeout) clearTimeout(timeout);
            timeout = setTimeout(function () {
                func.apply(context, args)
            }, wait);
        }
    }
    

    立即防抖

    注释:立即执行函数,然后开始计时,当计时结束后才能够再次执行函数。如果等待过程中该函数被调用,等待时间将刷新。

    function debounce(func, wait) {
        var timeout = null;
        var context = this;
        var args = arguments;
        return function () {
            if (timeout) clearTimeout(timeout);
            var callNow = !timeout;
            timeout = setTimeout(function () {
                timeout = null;
            }, wait)
            if (callNow) func.apply(context, args)
        }
    }
    

    也可以将非立即执行版和立即执行版的防抖函数结合起来,实现最终的双剑合璧版的防抖函数。

    /**
    * @desc 函数防抖
    * @param func (function) 函数
    * @param wait (number) 延迟执行毫秒数
    * @param immediate (boolean) true 表立即执行,false 表非立即执行
    */
    function debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this;
            var args = arguments;
            if (timeout) clearTimeout(timeout);
            if (immediate) {
                var callNow = !timeout;
                timeout = setTimeout(function () {
                    timeout = null;
                }, wait)
                if (callNow) func.apply(context, args)
            }
            else {
                timeout = setTimeout(function () {
                    func.apply(context, args)
                }, wait);
            }
        }
    }
    

    大神代码

    // 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, result;
    
        var later = function (context, args) {
            timeout = null;
            if (args) result = func.apply(context, args);
        };
    
        var debounced = restArgs(function (args) {
            if (timeout) clearTimeout(timeout);
            if (immediate) {
                var callNow = !timeout;
                timeout = setTimeout(later, wait);
                if (callNow) result = func.apply(this, args);
            } else {
                timeout = _.delay(later, wait, this, args);
            }
    
            return result;
        });
    
        debounced.cancel = function () {
            clearTimeout(timeout);
            timeout = null;
        };
    
        return debounced;
    };
    

    throttle(节流)

    节流分为时间戳定时版本

    如果一个函数持续的,频繁的触发,那么就让他在一定的时间间隔后触发。

    高频事件:
    onscroll oninput resize onkeyup onkeydown onkerpress
    onkeyup:每键入一个字母触发一次(并不是按照我们输入的汉字计算的)

    节流单纯的降低代码执行的频率,保证一段时间内核心代码只执行一次。

    时间戳版和定时器版的节流函数的区别就是,时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。

    时间戳版

    function throttle(func, wait) {
        var previous = 0;
        return function () {
            var now = Date.now();
            var context = this;
            var args = arguments;
            if (now - previous > wait) {
                func.apply(context, args);
                previous = now;
            }
        }
    }
    
    
    

    定时器版本

    function throttle(func, wait) {
        var timeout;
    
        return function() {
            var context = this;
            var args = arguments;
            if (!timeout) {
                timeout = setTimeout(function(){
                    timeout = null;
                    func.apply(context, args)
                }, wait)
            }
    
        }
    }
    
    
    

    可以将时间戳版和定时器版的节流函数结合起来,实现双剑合璧版的节流函数。

    /**
    * @desc 函数节流
    * @param func (function) 函数
    * @param wait (number) 延迟执行毫秒数
    * @param type  (number) 1 表时间戳版,2 表定时器版
    */
    function throttle(func, wait ,type) {
        if(type===1){
            var previous = 0;
        }else if(type===2){
            var timeout;
        }
    
        return function() {
            var context = this;
            var args = arguments;
            if(type===1){
                var now = Date.now();
    
                if (now - previous > wait) {
                    func.apply(context, args);
                    previous = now;
                }
            }else if(type===2){
                if (!timeout) {
                    timeout = setTimeout(function(){
                        timeout = null;
                        func.apply(context, args)
                    }, wait)
                }
            }
    
        }
    }
    
    
    
    • 大神代码
    // 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 timeout, context, args, result;
        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; //显示地释放内存,防止内存泄漏
        };
    
        var throttled = 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;
        };
    
        throttled.cancel = function () {
            clearTimeout(timeout);
            previous = 0;
            timeout = context = args = null;
        };
    
        return throttled;
    };
    
    
    

    总结

    throttle 和 debounce 均是通过减少实际逻辑处理过程的执行来提高事件处理函数运行性能的手段,并没有实质上减少事件的触发次数。比如说,我搜索时,onkeyup 该几次还是几次,只是我的请求变少了,处理的逻辑少了,从而提高了性能。

    参考

    https://juejin.im/post/5b651dc15188251aa30c8669
    以及其他文章~~~~~

  • 相关阅读:
    微信小程序捕获async/await函数异常实践
    从微信小程序开发者工具源码看实现原理(四)-
    从微信小程序开发者工具源码看实现原理(二)-
    从微信小程序开发者工具源码看实现原理(三)-
    从微信小程序开发者工具源码看实现原理(一)-
    webpack生成的css文件background-image url图片无法加载
    ubuntu创建快捷方式
    docker 安装常用数据库
    MySQL order by limit 分页数据重复问题
    docker安装redis
  • 原文地址:https://www.cnblogs.com/qq3279338858/p/14450635.html
Copyright © 2011-2022 走看看