doudu7626 2013-08-07 18:10
浏览 48
已采纳

PHP无限滚动拉随机结果?

I have a page that returns 16 records from my table.

As the user scrolls to the bottom, I then pull another 12 records from my table and append them to my previous results, my problem is that my results are being duplicated, and not in the correct order.

JS

    // Ajax Getentries.php
    var url = location.pathname;
    if(url.indexOf('missing-people.php') > -1) {
        didScroll = false;
        $(window).scroll(function () {
            didScroll = true;
        });
        setInterval(function () {
            if(didScroll) {
                didScroll = false;
                if(($(document).height() - $(window).height()) - $(window).scrollTop() < 100) {
                    var number = $(".directory").children().length;
                    $.ajax({
                        type: "POST",
                        url: "getentries.php",
                        data: "count=" + number,
                        success: function (results) {
                            $('.directory').append(results).fadein();
                        }
                    });
                }
            }
        }, 250);
    }

PHP

    $result = mysql_query("SELECT * FROM directory LIMIT {$_POST['count']},12");

    $c = 1;
    while($row = mysql_fetch_array($result))
      {
     echo '<div class="entry';
                     if (($c % 4) == 1) echo ' alpha ';
                echo ' span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
                echo '<a href="/missing-people/' . strtolower($row["Location_County_Last_Seen"]) . '/' . strtolower($row["First_Name"]) . '-' . strtolower($row["Surname"]) . '/' . $row["ID"] . '"><img src="/access/upload/' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', missing since ' . $row['Date_Last_Seen'] . ' " /></a>';
                echo '<span class="missing-from">Last seen in ' . ucwords($row["Location_County_Last_Seen"]) . '</span><a href="/missing-people/' . strtolower($row["Location_County_Last_Seen"]) . '/' . strtolower($row["First_Name"]) . '-' . strtolower($row["Surname"]) . '/' . $row["ID"] . '">View Profile</a></div>';       
                $c++;

      }

    mysql_close($con);
  • 写回答

1条回答 默认 最新

  • doujubeng2942 2013-08-07 18:30
    关注

    it seems like it's a race condition. Indeed, if you quickly scroll the page this is what happens:

    1. the setInterval gets triggered
    2. there are already 12 images in the dom, hence var number = $(".directory").children().length is 12.
    3. now new data is fetched from the server with ajax.

    Now, if I'm still at the bottom of the page and the ajax calls hasn't completed yet, the setInterval is going to be triggered again, number still resolve to 12 and I'm going to do the same ajax request. Hence duplication.

    Solving the issue could be as simple as setting number at the beginning of the script and incrementing it every time you make an ajax call.

        // Ajax Getentries.php
    var url = location.pathname;
    var number = $(".directory").children().length;
    if(url.indexOf('missing-people.php') > -1) {
        didScroll = false;
        $(window).scroll(function () {
            didScroll = true;
        });
        setInterval(function () {
            if(didScroll) {
                didScroll = false;
                if(($(document).height() - $(window).height()) - $(window).scrollTop() < 100) {
                    number += 12;
                    $.ajax({
                        type: "POST",
                        url: "getentries.php",
                        data: "count=" + number,
                        success: function (results) {
                            $('.directory').append(results).fadein();
                        }
                    });
                }
            }
        }, 250);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应