簡體   English   中英

有人能解釋一下這個 ajax 代碼的作用嗎?

[英]Can someone explain what this ajax code does?

有人能解釋一下這個 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);

這個 function 每秒都在遞歸調用自己。 它向Import.ff發送一個 AJAX GET 請求並傳遞 3 個查詢字符串參數: do=progressInfotype=sampletype和一個隨機數。 這個隨機數被附加到 url 中,因為 GET 請求被瀏覽器緩存,這樣可以確保它在每次請求時從服務器獲取最新內容。

服務器本身發送一個 XML 文件作為響應。 此 XML 文件包含一些節點,例如:

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

所以腳本在AJAX請求的成功回調中解析了這個XML。 它試圖獲取runningprogress節點的值。 如果running=true則檢查是否有進度節點並對其進行一些處理。 最后它在 1 秒后使用setTimeout function 調用自己。 等等。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM