简体   繁体   English

为什么“无限滚动”不会停止加载内容?

[英]Why doesn't the 'infinite scroll' stop loading content?

I am using an 'infinite scroll' script that keeps sending requests to the database even when there are no more records. 我正在使用“无限滚动”脚本,即使没有更多记录,该脚本也会不断向数据库发送请求。 When that happens, it reloads the last set on the page. 发生这种情况时,它将重新加载页面上的最后一组。 I would like for the function to stop running when it reaches the last record on the database. 我希望函数在到达数据库的最后一条记录时停止运行。 I'm new to JS so this is a bit difficult for me to troubleshoot, also, I'm not using jQuery. 我是JS的新手,所以这对我来说很难解决,而且我没有使用jQuery。 I am doing most of the work in the PHP script. 我正在PHP脚本中完成大部分工作。

I've been reading a lot of posts in here about 'infinite scroll' and I am unable to get how other people check the limits in JS. 我在这里阅读了很多有关“无限滚动”的文章,但我无法理解其他人如何检查JS中的限制。

JavaScript 的JavaScript

function loadPosts(){
    var target = document.getElementById('PostsContainer');
    var contentHeight = target.offsetHeight;
    var yOffset = window.pageYOffset;
    var y = yOffset + window.innerHeight;
    if(y >= contentHeight){
        var xhr = ajaxObj("POST", "loadContent.php");
        xhr.onload = function(){
            target.innerHTML += xhr.responseText;
        }
        xhr.send("action=loadMore");
    }
}
window.onscroll = loadPosts;

PHP 的PHP

$sql = "SELECT * FROM posts WHERE post_type = 'a' ORDER BY post_date DESC LIMIT 2" //Original content on page

$totalPosts = 12; (query to DB)
$max = 1;
$current = 2; //Start on record after initial content

while($current < $totalPosts){
  $sql = "SELECT * FROM posts WHERE post_type = 'a' ORDER BY post_date DESC
  LIMIT $current, $max";
  $result = $db_link->query($sql);
  $posts_list += ... //Collect the data from DB
  $current++;
}
echo $posts_list;

No matter how many times I keep scrolling the new content keeps loading even after I run out of records in the DB. 无论我滚动多少次,即使在数据库中的记录用完之后,新内容也会继续加载。 Output keeps repeating every single time I get to the bottom of the page. 每当我到达页面底部时,输出就会不断重复。 In this case I have 7 posts in the DB I start with 7, 6... then I keep getting posts 5-1. 在这种情况下,我在数据库中有7个帖子,我从7、6开始...然后我不断收到5-1。

So in this case what you can do, just add one parameter in json, from php or server side which will tell, is data present or not, based on that, you can stop calling loadPosts function 因此,在这种情况下,您可以做的是,只需从phpserver side在json中添加一个参数,该参数将告诉您是否存在数据,基于此,您可以停止调用loadPosts函数

so basically Algorithm be like, 所以基本上算法就像

...php ... php

while($current < $totalPosts){
......................
......................

  if($current >= $totalPosts)
  {
     $getNext = False;
  }
  else
  {
   $getNext = True;
  }
}

...javasrcipt ... javasrcipt

function loadPosts(){

   if(!getNext)
     return false;
   else
   {
   ......................
   ......................
   }
}
window.onscroll = loadPosts;

Hope this strategy will help you 希望这个策略对您有帮助

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

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