-1

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

这是代码:

<?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();


        }
        ?>  

提前致谢!我希望有一个人可以帮助我 :)

4

2 回答 2

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;
               }
            }
        }); 

    });

编辑:我改变了退出,返回假......因为退出可能会破坏jquery..不确定是否测试适合你的鞋子;)......也许会更好地使用而不是if......哦

于 2012-08-25T20:14:34.080 回答
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();
        }
    }); 

});
于 2012-08-25T20:22:51.067 回答