前端节流函数

//期间执行一次 防抖
var throttle = {
    timer: null,
    call: function (fn) {
        fn()
    },
    setCall: function (fn, delay) {
        clearTimeout(this.timer);
        var that = this;
        this.timer = setTimeout(function () {
            that.call(fn)
        }, delay)
    }
}
//期间间隔执行 节流
var debonuce = {
    timer: null,
    call: function (fn) {
        fn();
    },
    setCall: function (fn, delay) {
        var that = this;
        if (!this.timer) {
            this.timer = setTimeout(function () {
                clearTimeout(this.timer);
                that.call(fn)
            }, delay)
        }
    }
}

你可能感兴趣的:(算法与数据结构)