Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Polling is a bit harder than a simple while : just because generally all things you output to the browser will be interpreted when complete. Your example is quite clear :</p> <pre><code>success:function(data) { var json = data; $("#commidwin").append(json['msg']); last_msg_id = json["last_msg_id_db"]; setTimeout("load_msgs()", 1000); }, </code></pre> <p>jQuery will wait until the response is complete to build your <code>data</code> variable and then will call your success callback.</p> <p>One way to create long-polling is to have a task and a follower :</p> <ul> <li><p>the task is the "infinite" loop, it displays nothing but just catch and trigger events, put in a "box".</p></li> <li><p>the follower is an ajax call made every X seconds, it looks inside the "box" filled by the task, and immediately act inside the page.</p></li> </ul> <p>Here is an example of long-polling, there is no follower, just an event (release) that stops the poll, but you'll get the idea :</p> <pre><code>&lt;?php // For this demo if (file_exists('poll.txt') == false) { file_put_contents('poll.txt', ''); } // If this variable is set, a long-polling is starting... if (isset($_GET['poll'])) { // Don't forget to change the default time limit set_time_limit(120); date_default_timezone_set('Europe/Paris'); $time = time(); // We loop until you click on the "release" button... $poll = true; $number_of_tries = 1; while ($poll) { // Here we simulate a request (last mtime of file could be a creation/update_date field on a base) clearstatcache(); $mtime = filemtime('poll.txt'); if ($mtime &gt; $time) { $result = htmlentities(file_get_contents('poll.txt')); $poll = false; } // Of course, else your polling will kill your resources! $number_of_tries++; sleep(1); } // Outputs result echo "Number of tries : {$number_of_tries}&lt;br/&gt;{$result}"; die(); } // Here we catch the release form if (isset($_GET['release'])) { $data = ''; if (isset($_GET['data'])) { $data = $_GET['data']; } file_put_contents('poll.txt', $data); die(); } ?&gt; &lt;!-- click this button to begin long-polling --&gt; &lt;input id="poll" type="button" value="Click me to start polling" /&gt; &lt;br/&gt;&lt;br/&gt; Give me some text here : &lt;br/&gt; &lt;input id="data" type="text" /&gt; &lt;br/&gt; &lt;!-- click this button to release long-polling --&gt; &lt;input id="release" type="button" value="Click me to release polling" disabled="disabled" /&gt; &lt;br/&gt;&lt;br/&gt; Result after releasing polling : &lt;div id="result"&gt;&lt;/div&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; // Script to launch polling $('#poll').click(function() { $('#poll').attr('disabled', 'disabled'); $('#release').removeAttr('disabled'); $.ajax({ url: 'poll.php', data: { poll: 'yes' // sets our $_GET['poll'] }, success: function(data) { $('#result').html(data); $('#poll').removeAttr('disabled'); $('#release').attr('disabled', 'disabled'); } }); }); // Script to release polling $('#release').click(function() { $.ajax({ url: 'poll.php', data: { release: 'yes', // sets our $_GET['release'] data: $('#data').val() // sets our $_GET['data'] } }); }); &lt;/script&gt; </code></pre> <h1>Demonstration : <a href="http://home.fuz.org/tests/poll.php" rel="nofollow">here</a>.</h1>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload