Ajax round-robin Request status (WeChat public account logging on to the website with parameter QR code), ajax round-robin

Source: Internet
Author: User

Ajax round-robin Request status (the public account logs on to the website with the parameter QR code) and ajax round-robin

The function to be implemented here is to log on to the website by scanning the QR code with the public number and parameters.

But obviously, if ajax continuously requests the server, this will increase the load on the server, therefore, this example uses the setInterval function of js to periodically call and execute an ajax function to request data to the server. However, if the request is successful or fails after a certain number of requests, use the clearinterval function to clear the timer.

The code and comments are as follows: (the backend uses 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 the scan code logon mode box $ ('# login '). click (function () {// if the user has logged on, return if (uid) {return;} // to open the modal box, use the remote option to remotely load data $ ('# loginmodel '). modal ({remote: "{: U ('user/login')}"}) ;}); // hide the modal box and clear the data $ ("# loginModel "). on ("hidden. bs. modal ", function () {$ (This ). removeData ("bs. modal ") ;}); // when the modal box is displayed, the system requests data from the server through timed return. The timer requests the server $ ('# loginModel') every three seconds '). on ('shown. bs. modal', function (e) {timer = setInterval (ajax_request, 3000) ;}); // ajax request function, function ajax_request () {I ++; // if the request has been 20 and the request has not been successful, it is forced to end and a prompt message is given, because every 3 seconds is called for 20 times, it takes about one minute if (I> 2018.20.0000('.login_info1'0000.html ('<span style = "color: red;"> logon timeout. if you need to log on, refresh the 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, 3000); return ;}, error: function (){}});} // refresh () {location. reload () ;}</script>

Now, the code is over. I hope it will 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.