Javascript/php implements comet (Ajax long polling) push function
simple description:
long polling process: the page initiates a server request, and then the server keeps connecting until the data returns. After the data is returned, the browser closes the connection and then another server request is launched. This process is maintained continuously during page opening.
[html] view plaincopy
[code]
.Lt; input id=.quot; btn.quot; type=.quot; button.quot. (empty ($_POST.#91;'time'.#93;)) exit ();
set_time_limit (0); / / / unlimited request timeout
$i=0;
while (true) {
{1; 500000 / > > > > > > > / / if the data is obtained, the data is returned to the customer service side immediately, and the request is
$rand=rand (1999);
if ($rand.lt; =900) {
$arr=array ('success'=.gt;.Quot; 1.quot;'name'=.gt; > > / / /); _encode ($arr);
echo $rand;
Exit ();
}
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / server ($_POST.#91;'time'.#93;) Rray ('success'=.gt;.Quot; 0.quot;'name'=.gt;'xiaocai','text'=.gt; $rand);
//echo json_encode ($arr); Ript] view plaincopy
[code] window.onload=function () {
var BTN = document.getElementById (.Quot; btn.quot;); Br / > console.log (.Quot; click.quot;);
var XHR = new XMLHttpRequest ();
xhr.onreadystatechange = function () {= = = 4) {{200 / > 300). Us = = = 304) {
msg.innerHTML = xhr.responseText;
event.target.click ();
}
else{
alert (xhr.status); {} / >} LearTimeout (ID);
}
}
xhr.open (.Quot; post.quot;.Quot; data.php.quot; true); {
xhr.abort ();
alert (.Quot; timeout.quot;);
}, 80000);
xhr.send (.Quot; time=80.quot;);
} / > > because of the discovery of the base network in the learning process. The data is implemented using plug-ins (jQuery, etc.), which is implemented in pure JS and is worth collecting.