其他分享
首页 > 其他分享> > 记防抖函数

记防抖函数

作者:互联网

记一下免得忘记

debounce(fn, time, immediate) {
            let timer;
            return function () {
                if (immediate) {
                    clearTimeout(timer);
                    const now = !timer;
                    timer = setTimeout(() => {
                        timer = null;
                    }, time);
                    if (now) {
                        fn.call(this);
                    };
                }
                else {
                    clearTimeout(timer);
                    timer = setTimeout(() => {
                        fn.call(this);
                    }, time);
                }
            };
        },

标签:函数,clearTimeout,timer,记防抖,call,time,setTimeout,fn
来源: https://blog.csdn.net/qllzs/article/details/122000186