2012-08-25 41 views
-1

我有一个代码,当我向下滚动我的页面,它会从数据库中加载更多的内容,但我的问题是当我滚动速度太快,内容从数据库副本,如果我用这个代码和结果来的是,这将是麻烦为我的用户,重复的内容与负载内容,同时向下滚动使用jQuery,PHP

这里是代码:

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


     } 
     ?> 

在先进的感谢!我希望有人能帮助我:)

+0

确保数据加载完整的方法http://api.jquery.com/jQuery.post/ – Breezer

回答

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

}); 
+0

谢谢@MMK!你是一个拯救生命的人! :)我会尝试分析你对代码所做的事情,或者你只是将boxid声明为全局的,而且这样做? – JakeWevDeb

+0

是的。我们需要将boxid声明为全局的,并且检查boxid等于最后一个消息框ID等于意味着我们需要停止/返回函数(因为在ajax载入期间,它们在滚动时获得先前的msg id,并且该时间全局值也包含相同的id。) 。 – MMK

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的..不知道,但测试都适合你的鞋子;)...也许虽然会更好地使用,而不是如果...哦,以及

+0

感谢您的回答,先生,我想知道具体是什么原因造成的复制数据内容如果我滚动速度过快或按住向下键,直到达到它的底部,内容仍然重复,我试过你的代码,但它仍然是相同的,你是什么意思,数据应该加载使用完整的方法api.jquery.com/jQuery.post – JakeWevDeb