复习 防抖 节流

防抖,节流函数使用不多,lodash完美封装好,开箱即用,我自己也要了解一下内部逻辑

防抖:在延时时间内 再次触发函数,以最后一次触发执行函数,比如点击事件,点击多次提交数据

function debounce(cb,delay){
        var timer
        return function(){
            clearTimeout(timer);
            var that = this
            var args = [].slice.apply(arguments);
            timer = setTimeout(function() {
                cb.apply(that,args)
            }, delay);
        }
    }

节流: 在延时时间内 最多触发一次 ,比如 鼠标mousemove scroll事件

function throttle(cb, delay) {
    var last, now;
    last = new Date().getTime();
    return function () {
        now = new Date().getTime();
        var args = [].slice.apply(arguments);
        if (+now - last >= delay) {
            cb.apply(this, args);
            last = now;
        }
    };
}

原文地址:https://www.cnblogs.com/hill-foryou/p/14648065.html