0

大家好,我的无尽页面 js 代码运行良好,但由于某种原因,它只触发第二页。我对找到的一些代码进行了一些更改以满足我的需求,但现在不是继续加载项目,而是在我的控制台记录状态时在第 2 页停止。这是我的代码,有人可以检查它并告诉我为什么这没有触发吗?至于我的分页,我使用的是 will_paginate gem,我得到了底部页面的链接,所以后面的 es 工作正常。先感谢您。

编辑 2:如果有人遇到我的问题,这是我的最终设置。

- content_for :scripts do
  :javascript
    (function() {
      var divContainer = $('#posts-container');
      var page = 1,
          loading = false;
      var current_url = divContainer.data('url') + '?page=';

      function nearBottomOfPage() {
        return $(window).scrollTop() >= $(document).height() - $(window).height() - 10;
      }

      $(window).scroll(function(){
        if (loading) {
          return;
        }

        if(nearBottomOfPage()) {
          loading=true;
          page++;
          $.ajax({
            url: current_url + page,
            type: 'get',
            dataType: 'script',
            success: function(html) {
              var $items = $(html).find('.item');
              jQuery(".content").append($items).masonry( 'appended', $items, true );
              $(".content").masonry('reload');
            },
            error: function() {
              console.log("Error")
            },
            complete: function() {
              loading = false;
            }
          });
        }
      });
    }());

编辑1:

我发现 nearBottomOfPage() 函数无法正常工作。它只触发第一次,并且永远不会再次返回 true。为什么会这样?

4

1 回答 1

2

我注意到的一件事是您将“正在加载”标志设置为true

if(nearBottomOfPage()) {
  loading=true;

但您从未将其设置回false. 这意味着您的nearBottomOfPage()支票只会被触发一次。

尝试更新您的 AJAX 成功处理程序以重置loading

success: function(html) {
  var $items = $(html).find('.item');
  $(".content").append($items).masonry( 'appended', $items, true );
  $(".content").masonry('reload');
  console.log("Reloaded masonry");
  loading = false; // <----------------------------- This is sort of important.
}

您可能还想向 AJAX 添加错误处理程序。然后你可以将 移动loading = false;complete回调:

success: function(html) {
  // As you have it now...
},
error: function() {
  // Complain or something...
},
complete: function() {
  loading = false;
}
于 2012-06-16T19:01:52.420 回答