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>