开发者

AJAX Jquery refreshing ALL content but I want NEW content only

开发者 https://www.devze.com 2023-02-28 06:33 出处:网络
I may be reinventing the wheel but that\'s intentional. I am trying to make a real-time chat application using PHP HTML MySQL CSS. It\'s working quite well over the network and grabbing new content ev

I may be reinventing the wheel but that's intentional. I am trying to make a real-time chat application using PHP HTML MySQL CSS. It's working quite well over the network and grabbing new content every second. In IE you can notice a flicker every second, seemingly it is pulling all that data again and again, even if there's no new content, and I don't like that.

Here is my refresh JS code:

 $(document).ready(function() {
     $("#data").load("data.php");
   var refreshId = setInterval(function() {
      $("#data").load('data.php');
   }, 1000);
   $.ajaxSetup({ cache: false });
});

data.php is just a while looping through all the database records and showing them:

    while ($row = mysql_fetch_array($result))
    {
    echo "<p><b>". $poster ."</b>: ". $message ."<br /><span style='font-size: 10px;'>". date('D d M - g:i:s a', strtotime($row['when'])) ."</span></p>";
    }

I've looked and looked on the internet for solutions, fading etc, I can't think of how to accomplish this, surely there is some nice and eas开发者_如何学Pythony and efficient way?


You should pass a time stamp to the server of the last update and only retrieve new content.


The best solution, that I personally use in my shoutboxes, is to set a variable in the loaded data with a global variable.

Example:

<script type="text/javascript">
var global_var = 1234;
</script>
<p>HTML CONTENT GOES HERE</p>

Then when you call data.php you can call it with this new parameter.

$("#data").load('data.php?var=' + global_var );

Your global_var can be anything, from last inserted ID, or last timestamp, or???


This one is what you need, you only have to modify it for your needs:

Refresh a div's content only if new content is added to the database

This will pass the time when the page was loaded, and will refresh the data only if new content has been added, see the first answer.

All the best ;).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号