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!