Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无休止的页面导轨_Javascript_Jquery_Ruby On Rails_Will Paginate_Jquery Masonry - Fatal编程技术网

Javascript 无休止的页面导轨

Javascript 无休止的页面导轨,javascript,jquery,ruby-on-rails,will-paginate,jquery-masonry,Javascript,Jquery,Ruby On Rails,Will Paginate,Jquery Masonry,大家好,我的无休止的页面js代码工作得很好,但出于某种原因,它只会触发第二个页面。我对找到的一些代码做了一些更改,以满足我的需要,但现在它不再继续加载项目,而是在控制台记录状态时停止在第2页。这是我的代码,有人能检查一下,告诉我为什么不触发吗?至于我的分页,我使用的是will_paginate gem,我得到了底部页面的链接,所以后面的es工作正常。先谢谢你 编辑2:如果有人遇到我的问题,这里是我的最终设置 - content_for :scripts do :javascript

大家好,我的无休止的页面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。为什么会这样?

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

if(nearBottomOfPage()) {
  loading=true;
但您从未将其设置回
false
。这意味着您的
nearBottomOfPage()
检查只会触发一次

尝试更新AJAX成功处理程序以重置加载

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.
}

非常感谢你!这个小东西起作用了:)我感谢你的帮助。祝你周末愉快。
success: function(html) {
  // As you have it now...
},
error: function() {
  // Complain or something...
},
complete: function() {
  loading = false;
}