Real-time chat room with Ajax long polling

Source: Internet
Author: User

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 ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.