使用jquery,php向下滚动时带有加载内容的重复内容

时间:2012-08-25 20:05:45

标签: php jquery scroll

我有一个代码,当我向下滚动页面时,它会从数据库中加载更多内容,但我的问题是当我滚动得太快,从数据库加载的内容重复,如果我使用此代码并且结果来了对我来说,这对我的用户来说很麻烦,

这是代码:

<?php
include('../connection.php');

$action = @$_POST['action'];
$boxid = @$_POST['boxid'];

if($action <> "get")
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link rel="stylesheet" href="scroll.css" type="text/css" />
<script type="text/javascript" src="../js/jquery-1.2.6.pack.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){

        function last_msg_funtion() 
        { 

           var boxid=$(".message_box:last").attr("id");
           var action = "get";
            $('div#last_msg_loader').html('<img src="bigLoader.gif">');
            $.post("loader.php", { boxid: boxid, action: action }, function(data){
                if (data != "") {
                $(".message_box:last").after(data);         
                }
                $('div#last_msg_loader').empty();
            });
        };  

        $(window).scroll(function(){
            if  ($(window).scrollTop() == $(document).height() - $(window).height()){
               last_msg_funtion();
            }
        }); 

    });
    </script>

</head>

<body>
<div align="center">

<?php
$sql=mysql_query("SELECT * FROM announcements ORDER BY announce_id DESC LIMIT 10");
while($row=mysql_fetch_array($sql))
        {
        $msgID= $row['announce_id'];
        $msg= $row['content'];
        $posted= $row['postedby'];
?>
<div id="<?php echo $msgID; ?>"  align="left" class="message_box" >
<span class="number"><?php echo $posted; ?></span><?php echo "<b>".$msgID."</b>"; echo $msg; ?> 
</div>

<?php
}
mysql_close();
?>

<div id="last_msg_loader"></div>
</div>
</body>
</html>

<?php
}
else
{

$last_msg_id=$boxid;
 $sql=mysql_query("SELECT * FROM announcements WHERE announce_id < '$boxid' ORDER BY announce_id DESC LIMIT 5");
 $last_msg_id="";

        while($row=mysql_fetch_array($sql))
        {
        $msgID= $row['announce_id'];
        $msg= $row['content'];
        $posted= $row['postedby'];
        ?>
        <div id="<?php echo $msgID; ?>"  align="left" class="message_box" >
<span class="number"><?php echo $posted; ?></span><?php echo "<b>".$msgID."</b>"; echo $msg; ?> 
</div>
<?php
}
mysql_close();


        }
        ?>  

先谢谢了!我希望有人可以帮助我:)。

2 个答案:

答案 0 :(得分:0)

试试这段代码......可能会起作用

$(document).ready(function(){

        function last_msg_funtion() 
        { 

           var boxid=$(".message_box:last").attr("id");
           var action = "get";
            $('div#last_msg_loader').html('<img src="bigLoader.gif">');
            $.post("loader.php", { boxid: boxid, action: action }, function(data){
                if (data != "") {
                $(".message_box:last").after(data);         
                }
                $('div#last_msg_loader').empty();
            }).complete(function() { return true; });
        };  

        $(window).scroll(function(){
            if  ($(window).scrollTop() == $(document).height() - $(window).height()){
               if(!last_msg_funtion()){
                   return false;
               }
            }
        }); 

    });

编辑:我更改了退出,返回false ...导致退出可能会破坏jquery ..不确定虽然测试任何适合你的鞋子;)...也许虽然会更好用如果......哦,好吧

答案 1 :(得分:0)

试试这个。它可以解决你的问题

$(document).ready(function(){
  var boxid; // declare global
    function last_msg_funtion() 
    { 

   if(boxid==$(".message_box:last").attr("id")) return false;  // check with previous id

       boxid=$(".message_box:last").attr("id");
       var action = "get";
        $('div#last_msg_loader').html('<img src="bigLoader.gif">');
        $.post("loader.php", { boxid: boxid, action: action }, function(data){
            if (data != "") {
            $(".message_box:last").after(data);         
            }
            $('div#last_msg_loader').empty();
        });
    };  

    $(window).scroll(function(){
        if  ($(window).scrollTop() == $(document).height() - $(window).height()){
           last_msg_funtion();
        }
    }); 

});