The traditional Ajax polling method, the customer service side at the user-defined time interval to the server to query the latest data. This kind of pull data takes a short time interval to ensure the accuracy of the data, but too short a time interval client sends multiple requests to the server in a short period of time.
Reverse Ajax, which is called long polling or comet. The server and customer service need to maintain a long-time request, which allows the server to return messages to the client when there is data.
Xhtml
- < Div id="msg"> div >
- < input id="btn"type="button" value="Test"/>
Jquery
This uses Ajax to request the data.php page to get the value of ' success ', requesting a time of 80 seconds. In these 80 seconds, if no return from the server ' success ' is kept in a connected state until there is a data return or ' success ' value of 0 to close the connection. The next request continues after the connection is closed.
- $ (function() {
- $ ("#btn"). Bind ("click", {btn:$ ("#btn")}, function(evdata) {
- $.ajax ({
- Type: "POST" ,
- DataType: "JSON" ,
- URL: "data.php" ,
- timeout:80000, //ajax Request Time-out time 80 seconds
- data:{time:"i"}, ///40 seconds after data is returned regardless of the result server
- Success: function (data,textstatus) {
- //Get data from the server, display the data and continue querying
- if (data.success=="1") {
- $ ("#msg"). Append ("
[With Data] "+data.text);
- Evdata.data.btn.click ();
- }
- //did not get data from the server, continue to query
- if (data.success=="0") {
- $ ("#msg"). Append ("
[No data] ");
- Evdata.data.btn.click ();
- }
- },
- //ajax Request timed out, continue querying
- Error: function (xmlhttprequest,textstatus,errorthrown) {
- if (textstatus=="Timeout") {
- $ ("#msg"). Append ("
[Timeout] ");
- Evdata.data.btn.click ();
- }
- }
-
- });
- });
-
- });
Php
Here is the infinite loop, the end condition of the loop is to get the return result to return the JSON data.
and accept the $_post[' time ' parameter to limit the cycle timeout and avoid excessive waste of resources. (browser close will not send a message to the server, use may have been looped down)
- if (emptyempty($_post[' time '])) Exit ();
- set_time_limit (0); //Unlimited Request time-out
- $i = 0;
- while (true) {
- //sleep (1);
- Usleep (500000); //0.5 sec
- $i ++;
-
- //If the data is available, return the data to the customer service immediately and end the request.
- $rand =rand (1,999);
- if ($rand<=15) {
- $arr = Array (' success '= '1',' name '='xiaocai ' ,' text '=$rand);
- Echo Json_encode ($arr);
- Exit ();
- }
-
- //server ($_post[' time ']*0.5) seconds to tell the customer service end no data
- if ($i= =$_post[' time ']) {
- $arr = Array (' success '= '0',' name '= = ' Xiaocai ' , ' text ' = $rand );
- Echo Json_encode ($arr);
- Exit ();
- }
- }
Run effect: In the graph can see the request time of no data reached 40S, in the request of 40S to obtain the data request closes.
http://www.bkjia.com/PHPjc/445814.html www.bkjia.com true http://www.bkjia.com/PHPjc/445814.html techarticle The traditional Ajax polling method, the customer service side at the user-defined time interval to the server to query the latest data. This kind of pull data takes a short time interval to guarantee the data ...