节流与防抖,使用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)
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_44890362/article/details/122382844