Ajax to implement server and browser long connection function _ajax related

Source: Internet
Author: User

Sometimes, you need the server to actively push data to the browser, here with Ajax to achieve this function, specifically, see here:

<script type= "Text/javascript" src= "__css__/bootstrap-3.3.5-dist/js/bootstrap.min.js" ></script> <
  Script type= "Text/javascript" > var uid = "{$uid}";
  var i = 0;
  var timer; $ (). Ready (function () {//Open Sweep code login modal box $ (' #login '). Click (function () {//If user is logged in, return if (UID) {retur
      n;
    }//Open modal box, load data from remote by remotely option $ (' #loginModel '). Modal ({remote: "{: U (' User/login ')}"});
     
    });
    Empty the data after the modal box is hidden $ ("#loginModel"). On (' Hidden.bs.modal ', function () {$ (this). Removedata ("Bs.modal");
     
    }); When the modal box is displayed, the timer is requested to server $ (' #loginModel ') once every three seconds, by returning it periodically to request the data. On (' Shown.bs.modal ', function (e) {timer = Setinter
    Val (Ajax_request, 3000);
  });
   
  });
    Ajax request functions, function ajax_request () {i++; If you have requested 20 this does not request success, then force the end, give the hint information, because every 3s call once, for 20 times, is about a minute of the time if (i >) {$ ('. Login_info1 '). html (' <span style = "color:red;"
      > Login timeout, please refresh the page if required ~</span> '); Clearinterval (TimeR);
    return; } $.ajax ({type: "post", url: "{: U (' User/login_qrcode ')}", timeout:3000, data: {"Scen E_ID ": $ (' #scene_id '). Val ()}, Success:function (msg) {if (1 = msg.status) {$ ('. Login_inf O1 '). html (' <span style= ' color: #0C9; "
          > ' +msg.info+ ' </span> ');
          SetTimeout (refresh, 3000);
        return;
  }}, Error:function () {}});
  }//Overloaded page function refresh () {location.reload (); } </script>

The above is the entire content of this article, I hope to help you learn.

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.