Home  >  Article  >  Web Front-end  >  Tutorial on how to implement page polling with JS SetInterval

Tutorial on how to implement page polling with JS SetInterval

巴扎黑
巴扎黑Original
2017-08-12 16:32:341972browse

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])

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");
  }
 }

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!

Statement:
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