Home > Web Front-end > JS Tutorial > body text

Tutorial on how to implement page polling with JS SetInterval

巴扎黑
Release: 2017-08-12 16:32:34
Original
1979 people have browsed it

setInterval is a function that implements scheduled calls. It can call functions or calculate expressions according to a specified period (in milliseconds). Let’s share with you the JS SetInterval code to implement page polling through this article. Friends who are interested should take a look together

Concept introduction

setInterval is A function that implements scheduled calls, which can call functions or calculate expressions according to a specified period (in milliseconds). The setInterval method will continue to call the function until clearInterval is called or the window is closed.

The ID value returned by setInterval can be used as a parameter of the clearInterval method.

Tip: 1000 milliseconds = 1 second.

flash usage (from Baidu Encyclopedia)

setInterval action is to call the function at certain intervals when playing animation. method or object. You can use this action to update variables from the database or update the time display. The syntax format of the setInterval action is as follows:


setInterval(function,interval[,arg1,arg2,......argn])
setInterval(object,methodName,interval[,arg1,arg2,.....argn])
Copy after login

Syntax

Implementation code (monitoring payment status)


$(document).ready(function(){
  var timer = setInterval(function(){ajax_wx_pay_status(timer)},3000);
 });
 function ajax_wx_pay_status(timer) {
  var toUrl = "{:U('Order/ajax_get_pay_status')}";
  var orderUrl = "{:U('Member/myorder')}";
  if ($("#out_trade_no").val() != 0) {
   $.post(
     toUrl,
     {out_trade_no:$("#out_trade_no").val()},
     function (res) {
      if (res.status == 1) { //订单状态为1表示支付成功
       //此处可以进行相应业务代码的编写,例如支付成功提示,或者直接进行页面跳转.
       clearInterval(timer);
       //window.location.href = orderUrl; //页面跳转
      }
     },"JSON");
  }
 }
Copy after login

The above is the detailed content of Tutorial on how to implement page polling with JS SetInterval. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template