如何解决有人可以解释这个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);
解决方法
此函数每秒递归调用一次。它向
Import.ff
发送AJAX GET请求,并传递3个查询字符串参数:do=progressInfo
,type=sampletype
和一个随机数。由于GET请求是由浏览器缓存的,因此此随机数会附加到url上,从而确保每个请求都可以从服务器获取最新内容。
服务器本身发送XML文件作为响应。该XML文件包含一些节点,例如:
<importProgress>
<running>true</running>
<progress>20</progress>
</importProgress>
因此,脚本在AJAX请求的成功回调中解析此XML。它尝试获取running
和progress
节点的值。如果为“ 7”,那么它将检查是否存在进度节点并对其进行一些处理。最后,它在1秒钟后使用setTimeout
函数调用自己。等等。
因此,基本上,此脚本通过使用AJAX GET请求以1秒的间隔轮询服务器并解析响应来报告某些服务器操作的进度。