JS initiates long polling to get push messages

Source: Internet
Author: User

<!DOCTYPE HTML><HTMLLang= "en -us"><Head>    <MetaCharSet= "UTF-8">    <title></title>    <Scripttype= "Text/javascript"src= "Http://apps.bdimg.com/libs/jquery/1.8.1/jquery.min.js"></Script></Head><Body>    <Divclass= "tmp">123</Div>    <Scripttype= "Text/javascript">    //POST Request    functionClin_post () {$.post ("test.php",function(data) {$ ('. tmp'). HTML (data);        Clin_post (); },'JSON');    } clin_post (); </Script></Body></HTML>

A request is initiated again in the callback with JS initiating request to get a poll

PHP server can use time limit

<?PHP$start _time= Time();$num= 5;$diff _time= 0; while( ($num-$diff _time) >0 ) {    $now= Time(); $diff _time=$now-$start _time;}EchoJson_encode (Date(' y-m-d h:i:s '));?>

or directly with the sleep function.

<? PHP Sleep (5); echo json_encode (date(' y-m-d h:i:s '));? >

JS initiates long polling for push messages

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.