解决easyui jQuery JS的for循环调用ajax异步问题

因为JS的for循环与ajax非同步运行,所以致使for循环结束了而ajax却还未执行,解决此方法有两种php

一、设置ajax参数async为false,即与js同步,默认是true(异步).html

这里首先引用$.Ajax()中 async 和success的官方的解释:jquery

async Boolean Default: true
By default, all requests are sent asynchronous (e.g. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

代码示例:ajax

for(int i=0;i<x;i++){异步

var html = $.ajax({
url: "some.php",
async: false
}).responseText;async

}this

 

2.采用递归循环的方法解决此问题url

function func(times){
if(times <= 0){
return;
} spa

$.get(url,data,function(){

times --;
func(times); //递归调用
});
}htm

func(5);

相关文章
相关标签/搜索