无限滚动不断重复

时间:2019-05-24 04:53:04

标签: javascript php ajax

我最近在我的一个网站上实现了无限滚动,一切正常,但是问题是当我向下滚动到页面时,内容在重复,哪里出错了?任何想法

<div class="col-md-12" id="post-data">
<script type="text/javascript">
        $(window).scroll(function() {
            if($(window).scrollTop() + $(window).height() > $(document).height() - 100){
                var url = $(location). attr("href");
                var stuff = url.split('/');
                var last_id = stuff[stuff.length-2];
                //var last_id = $(".post-id:last").attr("id");

                loadMoreData(last_id);
            }
        });


        function loadMoreData(last_id){
          $.ajax(
                {
                    url: '/loadMoreData.php?last_id=' + last_id,
                    type: "get",
                    beforeSend: function()
                    {
                        $('.ajax-load').show();
                    }
                })
                .done(function(data)
                {
                    last_id= parseInt(last_id) + 8;
                    history.pushState({ foo: 'fake' }, 'Fake Url', 'viewquestions/' + parseInt(last_id) +'/scrolling');
                    $('.ajax-load').hide();
                    //console.log(data);
                    $("#post-data").append(data);
                    // hash=location.pathname;
                                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                      alert('server not responding...');
                });
        }
    </script>

我的loadMoreData.php

<?php
 require_once("include/config.php");
 $connection = mysqli_connect(DB_HOST, DB_USN, DB_PASSWORD, DB_DATABASE);
       $json = include('data.php');
       echo json_encode($json);
   mysqli_close($connection);
?>

我的data.php

<?php
$sql = "SELECT * FROM table
      WHERE id < '".$_GET['last_id']."' ORDER BY id DESC LIMIT 1";
$data = mysqli_query($connection,$sql);
    while($post = mysqli_fetch_array($data)){
      $id =$_GET['last_id'];

      functiona($question_id);
       functionb($question_id);
      }
?>

0 个答案:

没有答案