开发者

Can someone explain what this ajax code does?

Can someone explain what this ajax code does?

function ajaxProgress(){
 //Math.random() is for bitc开发者_如何学Gohy 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);


This function is calling itself recursively every second. It sends an AJAX GET request to Import.ff and passing 3 query string parameters: do=progressInfo, type=sampletype and a random number. This random number is appended to the url because GET requests are cached by browsers and by this it ensures that it gets fresh content from the server on each request.

The server itself sends an XML file as response. This XML file contains some nodes like:

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

So the script parses this XML in the success callback of the AJAX request. It tries to get the values of the running and progress nodes. If running=true then it checks whether there's a progress node and does some processing with it. Finally it calls itself 1 second later using the setTimeout function. And so on.

So basically this script reports progress from some server operation by polling the server at 1 seconds intervals using AJAX GET requests and parsing the response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜