我想通过Ajax根据用户选择的栏目ID生成页面,ID为多个
然后一个一个的传递到后台.ashx生成静态页面,把成功的值比如1传会前台,判断为1成功了之后再进行下一个ID,请问如何进行循环?贴出部分代码,等待补充
 var WebIDList = WebIDListStr.split(",");  $.ajax({
  url: "CreateHTML.ashx?action=createindex",
  type: "post",
  data: { "webid": WebIDList[0] }, //提交表单
  datatype: "text",
  async: true,
  success: function (data) {
  if (data == "1") {
  //成功,进行下一个,WebIDList[1]
  }
  },
  });只要解答就行了,不要问为何不一下把值传过去,再后台生成,因为一下子传过去,生成就会超时
使用async: false,可以实现我说的循环,但是浏览器没有相应。
请问既能实现循环,又能使浏览器相应该如何解决?

解决方案 »

  1.   


    var WebIDList = WebIDListStr.split(",");
    for(var i=0;i<WebIDList.length;i++){
      $.ajax({
      url: "CreateHTML.ashx?action=createindex",
      type: "post",
      data: { "webid": WebIDList[0] }, //提交表单
      datatype: "text",
      async: true,
      success: function (data) {
      if (data == "1") {
       continue;
      }else{
       break;
    }
      },
      });
    }
      

  2.   

    WebIDList[0] 为WebIDList[i] 
      

  3.   

    var WebIDList = WebIDListStr.split(",");
    var index=0;
    send();
    function send(){
    if(index==WebIDList.length){
    alert("ok");
    return;
    }
      $.ajax({
      url: "CreateHTML.ashx?action=createindex",
      type: "post",
      data: { "webid": WebIDList[index] }, //提交表单
      datatype: "text",
      async: true,
      success: function (data) {
      if (data == "1") {
      //成功,进行下一个,WebIDList[1]
    index++;
    send();
      }
      },
      });
    }
      

  4.   

    to wern0565
    break 和 continue 不能位于循环外。hch126163,不能继续执行吧。
      

  5.   

    异步队列模型
    看着个帖子 http://topic.csdn.net/u/20110228/16/36f298c2-cb92-48f9-9237-d6627fb7346f.html
      

  6.   

    谢谢 
    wern0565
    ,您的方法貌似可行
      

  7.   

    次序回调外面不能用枚举 这样明显就冲突 或者 重复了
    var WebIDList = WebIDListStr.split(",");function run(index){
      $.ajax({
      url: "CreateHTML.ashx?action=createindex",
      type: "post",
      data: { "webid": WebIDList[index] }, //提交表单
      datatype: "text",
      async: true,
      success: function (data) {
      if (data == "1") {
      //成功,进行下一个,WebIDList[1]
        index++;
        if(WebIDList.length>index){run(index);}
      }
      },
      });
    }
    if(WebIDList.lenght>0){
        run(0);
    }
      

  8.   

    1#用了FOR循环,3#用了递归!7#也一样是用递归!
    但是我觉得判断循环最好以$.ajax(内部的回调函数的值来判断是否继续循环,这样显得比较真实一点!不知道实际是否如此!
      

  9.   


    var WebIDList = WebIDListStr.split(",");
    send(WebIDList);
    function send(ids) {
    if (!ids) return;
    if (ids.length == 0) return;
    var id = ids.pop();
    $.ajax( {
    url : "CreateHTML.ashx?action=createindex",
    type : "post",
    data : { "webid" : id }, //提交表单
    datatype : "text",
    async : true,
    success : function(data) {
    if (data == "1") {
    send(ids);
    }
    }
    });
    }
      

  10.   

    不要在success里面调用下次请求,在complete里调用,然后设置超时处理,要不然中间一个卡住,下面的就都挂了
      

  11.   

    递归方法中最好不要有ajax请求,不要说递归了,就算两次的ajax嵌套,就容易产生一次长链接,用FF查看会看到这个请求一直不会断,就算是数据已经返回还是如此,这样就是一直对服务器请求着