2014-07-07 51 views
1

我有一個長時間運行的進程(不確定時間)在服務器端(運行數據庫中的查詢數量),這需要超過30秒。我想向用戶顯示%的進度。我在我的應用程序中使用jquery,struts。 有沒有辦法做到這一點?Jquery進度條服務器端

+0

http://tpeczek.blogspot.ie/2010/07/reporting-server-side-operation.html –

+0

http://stackoverflow.com/問題/ 12756047 /更新模式換進度條上,一長的Ajax查詢 –

回答

3

這就是我們如何做到的。

  • 當進程被觸發時,從客戶端創建一個GUID並將其傳遞給服務器。
  • 在服務器端運行進程,並在運行過程中,使用GUID作爲關鍵字,將進度存儲在會話/高速緩存中。
  • 在客戶端,定期對傳遞GUID值的服務進行ajax調用。該服務將返回與GUID值對應的進度狀態。
  • 根據服務返回的值更新ProgressBar狀態。
  • 如果您在會話中存儲該值,則一旦該過程完成,請確保清除它。

以下是ajax調用的示例方法。

function updateProgress() { 
     if (stopProgressCheck) return; 
     var webMethod = progressServiceURL + "/GetProgress"; 
     var parameters = "{'guid':'" + guid + "'}"; //passing the guid value 

     $.ajax({ 
      type: "POST", 
      url: webMethod, 
      data: parameters, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       if (msg.d != "NONE") { //add any necessary checks 
        //add code to update progress bar status using value in msg.d 
        statusTimerID = setTimeout(updateProgress, 100); //set time interval as required 
       } 
      }, 
      error: function (x, t, m) { 
       alert(m); 
      } 
     });  
    } 

希望這是對你有用:)