简体   繁体   English

我的 AJAX 调用只返回我的数据库的一部分,不会在滚动时加载更多

[英]My AJAX call only returns partial of my database and won't load more on scroll

I've made an AJAX script which loads data from my database on scroll.我制作了一个 AJAX 脚本,它在滚动时从我的数据库加载数据。 I have 24 records in my database, but when I scroll down it only seems to load 20 of them and stops there, instead of loading the remainder of the 4 records.我的数据库中有 24 条记录,但是当我向下滚动时,它似乎只加载了 20 条记录并停在那里,而不是加载 4 条记录的其余部分。

I've tried playing around with the offset and limit I've set on loading the records, but I can't seem to get it right.我已经尝试使用我在加载记录时设置的偏移量和限制,但我似乎无法正确设置。 When I resize my browser, it does seem to load everything which is odd.当我调整浏览器的大小时,它似乎加载了所有奇怪的东西。

My HTML are just 2 basic div's我的 HTML 只是 2 个基本的 div

<div class="articles">

        </div>
    <div class="articlesFade">

    </div>

And my JavaScript is我的 JavaScript 是

$(document).ready(function(){
    var increaseArticles = 0;
//get the first 10 articles from the database query in database.php
    $.ajax({
        type: "GET",
        url: "php/database.php",
        data: {
            'offset': 0,
            'limit': 10
        },
        success: function(data){
            $('.articles').append(data);
            increaseArticles += 10;
        }
    });

//scroll down to load the next batch of 10
    $(window).scroll(function(){
        if($(window).scrollTop() >= $(document).height() - $(window).height()){
            $.ajax({
                type: "GET",
                url: "php/database.php",
                data: {
                    'offset': increaseArticles,
                    'limit': 10
                },
                success: function(data){
                    $('.articlesFade').append(data).hide().fadeIn(1000);
                    increaseArticles += 10;
                }
            });
        }
    });
});

I think the problem might the scrollTop call, but I've not found any other way of achieving a "lazyloader" effect.我认为问题可能出在 scrollTop 调用上,但我还没有找到任何其他方式来实现“lazyloader”效果。 I want to program it myself, that's why I haven't used a plugin.我想自己编程,这就是我没有使用插件的原因。 Hopefully it's not some small rookie mistake, because I've spent way too much time on this little thing already希望这不是菜鸟的小错误,因为我已经在这件小事上花费了太多时间

scroll event is firing something like 10-15 times in a single small wheel action. scroll事件在单个小轮动作中触发 10-15 次。

So it fires just too often to have an async Ajax request in between each of these event.因此,在这些事件中的每一个之间都有异步 Ajax 请求时触发太频繁了。

Result is increaseArticles gets increased to fast by concurring requests.结果是increaseArticles通过并发请求快速增加。

So try using a "flag" to limit those requests...所以尝试使用“标志”来限制这些请求......

See comments in code.查看代码中的注释。

$(document).ready(function(){
  var increaseArticles = 0;
  //get the first 10 articles from the database query in database.php
  $.ajax({
    type: "GET",
    url: "php/database.php",
    data: {
      'offset': 0,
      'limit': 10
    },
    success: function(data){
      $('.articles').append(data);
      increaseArticles += 10;
    }
  });

  // Flag
  var ajaxRunning=false;
  var allResultsReceived=false;
      
  //scroll down to load the next batch of 10
  $(window).scroll(function(){
    if($(window).scrollTop() >= $(document).height() - $(window).height()){
        
      // If there is no Ajax request pending
      if(!ajaxRunning && !allResultsReceived){
        $.ajax({
          type: "GET",
          url: "php/database.php",
          data: {
            'offset': increaseArticles,
            'limit': 10
          },
          success: function(data){
            $('.articlesFade').append(data).hide().fadeIn(1000);
            increaseArticles += 10;
            
            var howManyResult = (data.match(/.column/g) || []).length;
            
            if(howManyResult!=10){
              allResultsReceived=true;    // Disable all future request call.
            }
            
            // Reset flag;
            ajaxRunning=false;
          }
        });
      
        // Set flag to true to prevent concurring ajax resquest.
        ajaxRunning=true;
      }
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM