页面上一段时间内通过ajax轮询后台数据的简单实现方法

来源:互联网 发布:淘宝怎么改好评为差评 编辑:程序博客网 时间:2024/05/16 01:06


var beginDate;
var loopMaxTime;

function doBegin(){
loopMaxTime = $("#loopMaxTime").val();

    var value1 = $("#param1").val();
    var value2 = $("#param2").val();

        $.ajax({
      type: "POST",
      url: contextPath + "/test1.do?t="+new Date(),
      dataType: "json",
      async: false,
      data: {"param1":value1,"param2":value2},
      success: function(data){
              if( data.SUCCESS ){
               beginDate = new Date();
              setTimeout("doQueryResult("+data.param3+")",10);
                }else{
                 alert(data.MSG);
                 
                return ;
               }
               
      },
      error:function (XMLHttpRequest, textStatus, errorThrown) {
       alert(errorThrown);
      }
   });      
     
}

 

function doQueryResult(param3)
{
           $.ajax({
        type: "POST",
        url: contextPath + "/test3.do?t="+new Date(),
        dataType: "json",
        async: false,
        data: {"param3":param3},
        success: function(RESULTDATA){
     
                if( RESULTDATA.SUCCESS ){
                   if(RESULTDATA.HASDATA)
                   {
                
                 //do submit
                   document.getElementById('submitBtn').submit();
                  //
                   }
                   else
                   {
                        var endDate = new Date();
                        if(endDate.getTime() - beginDate.getTime() > loopMaxTime)
                        {

                          alert("result is not done,time out.");
                        }else
                        {
                    
                       setTimeout("doQueryResult("+RESULTDATA.param3+")",5000);
                      
                        }
    
                   }
              
                }else{
                 alert(RESULTDATA.MSG);
                return ;
               }
        },
       error:function (XMLHttpRequest, textStatus, errorThrown) {
                 alert(errorThrown);
               }
     });
     

原创粉丝点击