How to refresh or reload a page in jquery mobile?

How can I slide in new posts from mysql table like Quora does using jquery?

  • I'm using jQuery to refresh posts from the database. Here's the jQuery: <script> jQuery(function($){ $('#loaddiv').show(); }) var auto_refresh = setInterval( function() { $('#loaddiv').hide().load('reload.php').show(); }, 5000); </script> And reload.php: <?php session_start();    ini_set('display_errors', 'On'); error_reporting(E_ALL);    $mysql=mysql_connect('***','***','***');    mysql_select_db('jmtdy');    $result=mysql_query("select * from users where username='".$_SESSION['username']."'") or die(mysql_error()); $dbarray=mysql_fetch_assoc($result); $id=$dbarray['id']; $result1=mysql_query("select * from friendship where userid='".$id."'") or die(mysql_error()); $dbarray1=mysql_fetch_assoc($result1); $userid=$dbarray1['friendid'];    $result2=mysql_query("select * from posts where userid='".$userid."' or userid='".$id."' ORDER BY id DESC LIMIT 20") or die(mysql_error());  if(mysql_num_rows($result2) >=1) {  while($dbarray2=mysql_fetch_assoc($result2)) {   echo '<div class="homestatus"><a class="homeusername" href="account.php?user='.$dbarray2['username'].'">'.$dbarray2['fname'].' '.$dbarray2['lname'].':<a><p class="homepost">'.$dbarray2['post'].'</p><p class="hometime">'.$dbarray2['time'].'</p></div>';   }   }   else {   echo '<p class="homestatus">No new updates from your friends.</p>'; } ?> My question is, how can I slide in the new posts, and only the new posts, like Quora slides in new answers?

  • Answer:

    Basically, you maintain state on the frontend. I'm assuming you have some kind of id to identify each post uniquely. In the markup, you embed the id of the posts displayed. You then extract the latest id from the markup and pass it as a parameter to reload.php, and use that to query the database (thus returning only "new posts"). And finally, you could use something like jQuery's append() or prepend() to add the new posts instead of what you're using currently.

Rohit Nair at Quora Visit the source

Was this solution helpful to you?

Other answers

Take a look at this: http://plugins.jquery.com/project/timers. It is a jQuery plugin which provides a bit more abstraction and convenience over setTimeout. If you want a bit more than that (a.k.a server push) then try comet :)

Phaneesh Nagaraja

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.