繁体   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