简体   繁体   中英

Ajax: distinguish old posts and new posts

I have created a very simple blog at www.example.com with only one page. When I connect to www.example.com I see all posts inserted in the database ( in mysql ).

Now I want that every 60 seconds an ajax request should check in the database if there are new posts. If there are new posts these will be inserted at the top above the old posts.

This is my question:

How can I through Ajax retrieve only new posts ( and so distinguish old posts and new posts ) ?

You should pass back an on-page counter than remembers the last post ID. Then

 SELECT required_fields, more_required, etc 
 FROM posts 
 WHERE post_id > on_page_variable

So on page

var last_post_id = 32; // or whatever
$.getJSON('script.php', {'post_id': last_post_id }, function(data){
    $.each(data.post, function(post){
         $('div.post:first').before(post.html);
         last_post_id = post.id;
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM