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 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常