节流与防抖,使用ES6中的箭头函数来实现

1.说到节流防抖,相信大家都不陌生,其实之前我就有详细介绍过,大家可以去看我另外一篇文章

节流与防抖链接,我在这那里面使用es5实现的。

现在我使用es6的箭头函数去实现它。

节流的实现

        function throttle(callback, time) {
            let timer = null;
            return (...args) => {
                if (!timer) {
                    timer = setTimeout(() => {
                        callback.apply(this, args)
                        timer = null
                    }, time)
                }
            }
        }

防抖的实现

 function debounce(callback, time) {
            let timer = null;
            return (...args) => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    callback.apply(this, args)
                }, time)
            }
        }


版权声明:本文为qq_44890362原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。