打赏

防抖函数第一次立即执行

 

        function debounce(fn, wait) {
            var timer = null;
            var self = this;
            var args = arguments;
            var count = 0;
            return function () {
                clearTimeout(timer);
                if (count == 0) {// 第一次立即执行
                    fn.apply(self, args);
                    count ++;
                } else {
                    timer = setTimeout(function () {
                        fn.apply(self, args)
                        count ++;
                    }, wait);
                }

            }
        };

  

posted on 2020-09-12 16:52  jlyuan  阅读(1407)  评论(0编辑  收藏  举报

导航