it may be too late for me to see a clear solution here, but I thought that I would get some thoughts from someone with an opinion ...
The site I'm working on has a long list of user posts. I have all the scroll event handlers working in ajax in the next batch of 100 posts when you reach or are approaching the bottom.
my question is ... How to prevent the following scenario?
- UserX visits the site and downloads messages 1-100
- Another 10 users visit the site and add another 10 posts.
- UserX scrolls down and downloads messages 101-200, which used to be messages 91-190.
- UserX ends up duplicating 91-100 messages per page
I will include a stripped down version of my code below if it helps anyone else
$.ajax({ type:'POST', url:"/userposts.php", data:{ limit: postCount }, success:function(data) { $("postsContainer").append(data); if ( $("postsContainer").find("div[id='lastPostReached']") ) {
in my PHP script I have essentially the following:
if ( ! isset($_POST["limit"]) ) { $sql .= " LIMIT 101"; // initial request } else { $sql .= " LIMIT {$_POST["limit"]},101 } $posts = mysql_query($sql); while( $post = mysql_fetch_assoc($posts) ) { /* output formatted posts */ } // inform callback handler to disable infinite scrolling if ( mysql_num_rows($posts) < 101 ) { echo '<div id="lastPostReached"></div>'; }
This gives me endless scrolling beautiful and easy, but how can I prevent the hypothetical duplicates that will appear when new records are added to the table between ajax requests?
source share