Previous JS:
// Process Ajax long polling
$ (Function (){
Ask_order ();
Function ask_order (){
VaR ask_action = "{: U ('index/order_commet ', array ('time' => 10, 'desk _ id' => $ partition _id ))}";
$. Ajax ({
Type: "Get ",
Datatype: "JSON ",
URL: ask_action,
Timeout: 80000, // Ajax request timeout time 80 seconds
Success: function (data, textstatus ){
// Obtain data from the server, display the data, and continue querying
If (data. Status = "1 "){
// Alert (data. Data );
VaR html_str = "";
VaR result = data. Data;
$ (Result). Each (function (I ){
Html_str + = '<li>' + result [I]. goods_name + '& nbsp; <a data-order = "' + result [I]. order_id + '"data-Product ="' + result [I]. goods_name + '"class =" little_delete "href =" javascript:; "> Delete </a> </LI>'
});
Certificate ('.product_ordered'apps.html (html_str );
}
// If no data is obtained from the server, continue to query
If (data. Status = "0 "){
Console. Log ('no data ');
}
SetTimeout (ask_order, 5000); // you can customize the latency in 5000 milliseconds.
},
// The Ajax request times out and the query continues.
Error: function (){
SetTimeout (ask_order, 5000); // you can customize the latency in 5000 milliseconds.
}
});
}
})
Background PHP:
// Process the long round training method.
Public Function order_commet (){
If (empty ($ _ Get ['time']) {
Die ();
}
Set_time_limit (0 );
$ Limit_time = $ _ Get ['time'];
$ Pipeline _id = $ this-> pipeline _id;
$ I = 0;
While (true ){
Usleep (500000 );
$ I ++;
$ Result = $ this-> order_mod-> get_order_by_desk ($ region _id );
If ($ result ['result'] = 1 ){
$ This-> ajaxreturn (Array ('status' => 1, 'data' => $ result ['data']);
Die ();
}
If ($ I = $ limit_time |! $ Region _id ){
$ This-> ajaxreturn (Array ('status' => 0, 'data' => 'nodate '));
Die ();
}
}
}