2011-05-24 81 views
-1

有人可以解釋這個ajax代碼做什麼?有人可以解釋這個ajax代碼做什麼?

function ajaxProgress(){ 
//Math.random() is for bitchy ie to prevent caching the xml. 
$.get('sample.ff?do=progressInfo&type=sampletype&dummy='+Math.random(), { dataType: 'xml'},  function(xml) { 
//if the import is running get infos about the progress... 
if($('/importProgress/running', xml).text() == 'true') { 
//..there are no infos yet, so it was just started.. 
if($('/importProgress/progress', xml) == null || $('/importProgress/progress', xml).text() == ''){ 
//do something 
} 
.......... 
setTimeout("ajaxProgress()", 1000); 

回答

2

該函數每秒遞歸調用它自己。它發送一個AJAX GET請求到Import.ff並傳遞3個查詢字符串參數:do=progressInfo,type=sampletype和一個隨機數。這個隨機數被追加到url上,因爲GET請求被瀏覽器緩存,並由此確保它在每個請求中從服務器獲取新內容。

服務器本身發送一個XML文件作爲響應。此XML文件包含像一些節點:

<importProgress> 
    <running>true</running> 
    <progress>20</progress> 
</importProgress> 

所以腳本解析這個XML在Ajax請求的成功回調。它試圖獲取runningprogress節點的值。如果running=true那麼它會檢查是否有進度節點並對它進行一些處理。最後它在1秒後使用setTimeout函數自行調用。等等。

所以基本上這個腳本通過使用AJAX GET請求和解析響應以1秒的間隔輪詢服務器來報告某些服務器操作的進度。

+0

我還有一個問題,我可以在ajax代碼中添加類似修訂的東西,當修訂版發生變化時,它會保持一個軌道,然後執行某些操作。修訂版來自struts操作。 – 2011-05-24 20:32:13

相關問題