2013-08-30 100 views
1

我有这个加载消息,而我的ajax调用检索数据。但是我得到了奇怪的结果。要么消息出现,并且中途呈现,直到ajax完成或者完全出现,让用户想知道什么是错误的。我需要一个加载消息的原因是,当检索数据时,它的延迟时间大约为5-10秒,对话框打开后绘制地图,然后使用标签重绘地图的要素图层。使用jquery加载消息

这里是我的代码:

function loadData(v) 
{  
    var reg = 1; 
    var vId = v;      
    var d = 
    { 
     regionType: reg, 
     varId: vId 
    }; 

    //$("#loading").ajaxStart(function() { 
    // $(this).show(); 
    //}).ajaxStop(function() { 
    // $(this).hide(); 
    //}); 

    $("#loading").ajaxStart(function() { 
     $(this).show(); 
    }); 

    $.ajax({ 
     type: "GET", 
     url: WebRoot + "ws/bis.asmx/Data", 
     data: d, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) {     

      fipsData = data.d; 
      openBox(d); 
      init(regType, varId); 

      $("#loading").ajaxStop(function() { 
       $(this).hide(); 
      }); 

     } //ends success function 
    }); //ends ajax call   
}; //ends message 
+0

我不认为有什么理由在'ajaxStop()'方法中包装'hide()'方法 - 你使用的是成功函数,所以除非你的'openBox'或'init'方法是做一个Ajax调用,你可以在你的成功函数的最后一行调用'hide'。 –

回答

3

无需为ajaxStartajaxStop

function loadData(v) 
{  
    var reg = 1; 
    var vId = v; 
    var $loading = $("#loading");      
    var d = 
    { 
     regionType: reg, 
     varId: vId 
    }; 


    // Starts immediately after this line so no need to use ajaxStart 
    $loading.show(); 

    $.ajax({ 
     type: "GET", 
     url: WebRoot + "ws/bis.asmx/Data", 
     data: d, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) {     

      fipsData = data.d; 
      openBox(d); 
      init(regType, varId); 

     }, //ends success function 

     // Fires even if a failure, so loading spinner won't hang around for no reason 
     done: function() { 
      $loading.hide(); 
     } 
    }); //ends ajax call   
}; //ends message 
+0

我原来的答案错误地使用了'$(this)',当我相信你的意思是'#loading' –

+0

现在我工作的很好,我只需要平滑它并定位加载消息和背景谢谢 –

0

如果你在使用ajaxStopajaxStart设置,那么你应该移动ajaxStop处理器之外成功回调。

$("#loading").ajaxStart(function() { 
    $(this).show(); 
}).ajaxStop(function() { 
    $(this).hide(); 
}); 

    $.ajax({ 
     type: "GET", 
     url: WebRoot + "ws/bis.asmx/Data", 
     data: d, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) {     

      fipsData = data.d; 
      openBox(d); 
      init(regType, varId); 

     } //ends success function 
    }); //ends ajax call