简体   繁体   English

jquery ajax同步调用beforeSend

[英]jquery ajax synchronous call beforeSend

I have a function called: 我有一个叫做的函数:

function callAjax(url, data) {
 $.ajax(
   {
     url: url, // same domain
     data: data,
     cache: false,
     async: false, // use sync results
     beforeSend: function() {
       // show loading indicator
     },
     success: function() {
       // remove loading indicator 
     }
   }
  );
}

In the code, I call "callAjax" X number of times and I want to update the data synchronously. 在代码中,我多次调用“callAjax”X次,我想同步更新数据。 It is done as expected, but one problem: the loading item doesn't show in beforeSend function. 它按预期完成,但有一个问题:loading项目没有显示在beforeSend函数中。 If I turn async to true, it works but the updates aren't synchronously done. 如果我将async变为true,它可以正常工作,但更新不会同步完成。

I've tried several things with no success. 我尝试了几件事但没有成功。 I tried putting the loading indicator before the ajax call like this: 我尝试在ajax调用之前加载加载指示器,如下所示:

function callAjax(url, data) {
  // show loading div

  $.ajax(
    {
      // same as above
    }
   );
}

But for some reason it doesn't want to show the loading indicator. 但由于某种原因,它不想显示加载指标。 I notice a strange behavior when I put an "alert" in the beforeSend and the loading indicator appears in that case, but I rather not pop up a message box. 当我在beforeSend中输入“alert”并且在那种情况下出现加载指示符时,我注意到一个奇怪的行为,但我宁愿不弹出一个消息框。

Got any ideas? 有什么想法吗?

Making a synchronous call like that is like putting up an "alert()" box. 像这样进行同步调用就像放了一个“alert()”框。 Some browsers stop what they're doing, completely, until the HTTP response is received. 有些浏览器完全停止了他们正在做的事情,直到收到HTTP响应。

Thus in your code, after your call to the "$.ajax()" function begins, nothing happens until the response is received, and the next thing as far as your code goes will be the "success" handler. 因此,在您的代码中,在您调用“$ .ajax()”函数开始之后,在收到响应之前没有任何反应,并且您的代码所进行的下一件事将是“成功”处理程序。

Generally, unless you're really confident in your server, it's a much better idea to use asynchronous calls. 一般来说,除非你在你的服务器非常有信心,这是一个更好的主意,用异步调用。 When you do it that way, the browser immediately returns to its work and simply listens in the background for the HTTP response. 当你这样做时,浏览器立即返回其工作,只是在后台监听HTTP响应。 When the response arrives, your success handler will be invoked. 当响应到达时,将调用您的成功处理程序。

When you do the blocking I/O the program is halted until the the input is received, in JS words when doing a synchronous call, the program halts and browser window freezes (no painting can be done) until the response is received. 当您执行阻塞I / O时,程序将停止,直到收到输入,在执行同步调用时,JS语言中,程序停止并且浏览器窗口冻结(无法进行绘制),直到收到响应。 In most cases doing syncronus calls and any kind of blocking I/O can be avoided. 在大多数情况下,可以避免执行syncronus调用和任何类型的阻塞I / O. However imagine your doing a progress bar in java or any other programming language, you have to spawn a different thread to control the progress bar, I think. 但是想象一下你在java或任何其他编程语言中做进度条,你必须产生一个不同的线程来控制进度条,我想。

One thing to try in your case, is to call the ajax call after a time delay 在你的情况下尝试的一件事是在延迟一段时间后调用ajax调用

//loading div stuff, 
//if your doing some animation here make sure to have Sufficient 
//time for it. If its just a regular show then use a time delay of 100-200
setTimeout( ajaxCall, 500 );

EDIT ajaxcall in setTimeout, Example 在setTimeout中编辑 ajaxcall, 示例

This is what you are looking for - .ajaxStart() 这就是你要找的东西 - .ajaxStart()

It will be triggered when any ajax event starts 当任何ajax事件开始时它将被触发

http://api.jquery.com/ajaxStart/ http://api.jquery.com/ajaxStart/

They even give a specific example similar to what you are trying to accomplish: 他们甚至给出了一个类似于你想要完成的具体例子:

 $("#loading").ajaxStart(function(){
   $(this).show();
 });

You can then use the .ajaxStop() function 然后,您可以使用.ajaxStop()函数

$("#loading").ajaxStop(function(){
      $(this).hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM