Ajax Polling Request status

Source: Internet
Author: User
The function to be implemented here is to log in to the website by scanning the QR code of the public number with parameters. But obviously, if Ajax continuously requests the server, this will aggravate the load of the server, so this example uses the JS setinterval to periodically invoke the execution of an AJAX function to request data from the server. However, the timer is emptied with the Clearinterval function after a successful request or a certain number of requests.

The code and comments are as follows: (the backend is implemented with thinkphp, so the JS code contains some thinkphp syntax rules)


<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 the user is already logged in, return if (UID) {return;} Open the modal box and load the data remotely from $ (' #loginModel ') from the remote option. Modal ({remote: "{: U (' User/login ')}"}); /Modal box is hidden after emptying the data $ ("#loginModel"). On ("Hidden.bs.modal", function () {$ (this). Removedata ("Bs.modal");}); /When the modal box is displayed, request data to the server by a timed return, the timer requests the server $ (' #loginModel ') every three seconds. On (' Shown.bs.modal ', function (e) {timer = SetInterval ( Ajax_request, 3000);}); /ajax request functions, function ajax_request () {i++;//If you have requested 20 this no request succeeds, then force the end, give the prompt information, because every 3s call once, for call 20 times, probably a minute of time if (I > 20) {$ (' . Login_info1 '). html (' <span style= "color:red;" > Login timeout, to log in please refresh Page ~</span> '); clearinterval (timer); return;} $.ajax ({type: "post", url: "{: U (' User/login_qrcode ')}", Timeout:3000,data: {"scene_id": $ (' #scene_id '). Val ()}, Success:function (msg) {if (1 = = Msg.status) {$ ('. Login_info1 ').HTML (' <span style= ' color: #0C9; " > ' +msg.info+ ' </span> '); SetTimeout (refresh, n); return;}},error:function () {}});} Reload page function Refresh () {location.reload ();} </script>

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.