窗口滚动事件多次触发

时间:2015-07-09 01:50:26

标签: javascript backbone.js marionette

我正在使用Marionette.js应用程序,当用户向下滚动页面时,该应用程序会获取更多数据。

setupWindowScrollListener: function() {
            var $window = $(window),
                $document = $(document),
                that = this;

            $window.on('scroll', _.debounce(function() {
                var scrollTop = $window.scrollTop(),
                    wHeight = $window.height(),
                    dHeight = $document.height(),
                    margin = 100;

                if(scrollTop + wHeight > dHeight - margin) {
                    eventer.trigger('fetch:more:products');
                }
            }, 500));
        }

问题在于,当用户向下滚动到页面底部时,eventer会触发多个事件而不是一个事件。

更新

使用isLoading检查的代码

    setupWindowScrollListener: function() {
        var $window = $(window),
            $document = $(document),
            that = this,
            isLoading = false;

        $window.on('scroll', _.throttle(function() {
            if(!isLoading) {
                var scrollTop = $window.scrollTop(),
                    wHeight = $window.height(),
                    dHeight = $document.height(),
                    margin = 100;

                if(scrollTop + wHeight > dHeight - margin) {
                    isLoading = true;
                    eventer.trigger('fetch:more:products');
                    isLoading = false;
                }
            }
        }, 500));
    }

0 个答案:

没有答案