AJAX调用每一个()循环不会是异步的,里面 [英] Ajax Call inside a each() Loop not being asynchronous

查看:128
本文介绍了AJAX调用每一个()循环不会是异步的,里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做涉及在经历了一系列的复选框。

有关他们那,检查各一个,我必须执行一些计算(使用Ajax调用)。如果有错误,我要打破循环并返回一个消息。

每一个Ajax调用需要大约0.4秒,执行但永远不会成为一个问题,因为我只能有最多4人检查,在任何给定的时间。

不过,我现在面临的问题是,最后的警告框弹出,一个误差函数内部()之前。误差函数正常工作,但后最后一节似乎总是如此。

我下的是IM pression通过定义呼叫为异步=假,code执行会等到调用返回的响应。那是一个错误的假设?

有没有办法做到这一点?

在此先感谢

  $(输入[ID * ='chk_aut'])。每个(函数(){
  如果(THIS.VALUE ==上){
    $阿贾克斯({
      键入:GET,
      网址:testpage.asp
      缓存:假,
      异步:假的,
      数据:这=校验值和放大器;值=
          + i_value
          +&放大器; REF =+ $(#cb_ref​​)VAL()
          +&放大器; NIF =+ $(#txt_nif)VAL()
          +和天=+ i_day
          +与&一个月=+ i_month
          +&安培;小时=+ i_hours
          +&放大器;同期=+ i_year +&放大器; MS =
          +新的Date()。的getTime()
      成功:函数(MSG){
        //做一些事情,但继续循环
      },
      错误:函数(){
        i_result = FALSE;
        警报(发生错误);
        返回false; //打破循环
      }
    });
  }
});
警报(i_result);
 

解决方案

我认为你需要使用

 ,异步:假的
 

让你通过布尔的错误,不是字符串。这应该让你的AJAX调用同步的。

请参阅<一href="http://stackoverflow.com/questions/1531693/-ajax-async-false-request-is-still-firing-asynchronously">this问题获取类似的情况,一些更多的细节。特别是,您可能会发现,为同步AJAX调用您不能使用成功的处理程序,并且必须返回结果,一旦你的电话已经完成(你出了AJAX的范围自称)。

如果可能的话,你应该尽量调整你的code,以便它可以异步工作。同步执行并没有很好的转化为浏览器。

在你的情况,如果你使用的是每一个AJAX调用积累结果被检查的每个复选框,考虑将在一个窗口变量工作的价值,并跟踪其请求已经返回。当你确定你已经获得了成功响应回各的AJAX调用你开始,你可以脱掉你的警告框,或任何你想做的事情的结果。这就是你如何可以开始考虑异步只是一个例子。

What I'm trying to do involves going through a series of checkboxes.

For each one of them that it is checked, I have to perform some calculations (using an ajax call). If there is an error, I have to break the loop and return a message.

Every ajax call takes about 0,4 seconds to perform but that shall never be a problem because I can only have a maximum of 4 of them checked at any given time.

But the problem I'm facing is that, the final alert box pops-up before the one inside the error function(). The error function does work but it appears always after the final one.

I was under the impression that by defining the call as asynchronous=false, code execution would wait until the call returned a response. Is that a wrong assumption?

Is there a way to accomplish this?

Thanks in advance

$("input[id*='chk_aut']").each(function() {
  if(this.value=="on") {
    $.ajax({
      type: "GET",
      url: "testpage.asp",
      cache:"false",
      async:"false",
      data: "which=checkvalues&value="
          + i_value
          + "&ref="+$("#cb_ref").val()
          + "&nif="+$("#txt_nif").val()
          + "&day="+i_day
          + "&month="+i_month
          + "&hours="+i_hours
          + "&year="+i_year + "&ms="
          + new Date().getTime(),
      success: function(msg) {
        //do something but continue the loop
      },
      error: function() {
        i_result =false;
        alert("An error has occurred");
        return false;//break the  loop
      }
    });
  }
});
alert(i_result);

解决方案

I think you need to use

,async: false

So that you pass the boolean false, not the string "false". That should make your AJAX call synchronous.

See this question for a similar situation and some more details. In particular, you may find that for synchronous AJAX calls you can't use the success handler and must return results once your call has completed (you're out of the scope of the AJAX call itself).

If at all possible you should try to restructure your code so that it can work asynchronously. Synchronous execution does not translate well to the browser.

In your situation, if you are using each AJAX call to accumulate results for each checkbox that is checked, consider storing the working value in a window variable and keeping track of which requests have returned. When you determine that you have received a successful response back for each of the AJAX calls you initiated, you can kick off your alert box or whatever else you want to do with the results. That is just one example of how you can start thinking asynchronously.

这篇关于AJAX调用每一个()循环不会是异步的,里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆