Ajax polling request status (micro-public number with parameter two-dimensional code login site) _ajax related

Source: Internet
Author: User
Tags setinterval

Here to achieve the function is: by scanning Code micro-letter public number with parameters of two-dimensional code, to log on to the site.

But obviously, if Ajax uninterrupted request server, this will aggravate the load of the server, so this example uses JS setinterval to periodically invoke the implementation of an AJAX function to request data to the server, Use the Clearinterval function to empty the timer when the request succeeds or if a certain number of times is not successful.

Code and comments are as follows: (The backend adopts thinkphp implementation, 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 the IF (UID) {returns;}//Open modal box.
Remotely load data from remote 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 = SetInterval (
Ajax_request, 3000);
});
}); Ajax request Function Ajax_request () {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: {"scene_id": $ (' #scene_id '). Val ()}, suc Cess:function (msg{if (1 = msg.status) {$ ('. Login_info1 '). html (' <span style= ' color: #0C9; "
> ' +msg.info+ ' </span> ');
SetTimeout (refresh, 3000);
return;
}}, Error:function () {}}); }//Overloaded page function refresh () {location.reload ();} </script>

OK, the code is over, I hope to help you!

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.