来自等到第一个Ajax调用完成prevent第二Ajax调用第一个、Ajax、prevent

2023-09-10 19:07:19 作者:南巷烈酒独饮悲

我跑在页面加载两个Ajax调用。他们如果我在桌面浏览器中打开页面预期双方的工作。

不过,如果我在Android浏览器中打开同一个页面(浏览器为例),我注意到,第二AJAX功能的响应正在等待第一个AJAX功能,有点失败的目的完成异步的烦躁。两者都同时执行,但第二个函数的成功仅第一Ajax调用的成功完成后执行的功能。

截图

这是工作在桌面浏览器,而不是在Android的浏览器,这一事实让我相信一定有某种形式的机器人被阻塞并发异步调用设置的。

如果是这样的话,是有可能,我可以禁用?我的code是如下BTW:

  $(函数(){
       VAR intervalID = window.setInterval(函数(){
                doAjax(); //这是它正在等待第一个Ajax调用完成的功能
            },2000);

      //第一个Ajax调用
      $阿贾克斯({
    类型:'后',
    网址:进度insert.php',//主要是为插入记录到数据库
    成功:函数(数据)
    {
       clearInterval(intervalID);
    }
     });

     功能doAjax()
     {
    $阿贾克斯({
        类型:'后',
        网址:进步-update.php',//基本上返回多少条记录被插入至今
        成功:函数(数据)
        {
                // 做一点事
        }
    });
      }

});
 

解决方案

不幸的是所有的浏览器实现的JavaScript是单线程的,这意味着异步调用是异步的服务器,但不是同一个JavaScript和放大器;因此,等待老人的请求。你可以做什么来克服是推动在一个变量和放大器的要求;下一次检查,如果请求的状态不是200 OK /成功,不作任何进一步的要求相同的功能,样品code可以像:

  requests.push($。阿贾克斯({
        类型:'后',
        网址:进步-update.php',//基本上返回多少条记录被插入至今
        成功:函数(数据)
        {
              对于(VAR I = 0; I< requests.length;我++)
              请求[I] .abort();
        }
    }));
 
掌握AJAX之AJAX通讯技术简介

I'm running two ajax calls on page load. They both work as expected if I open the page in desktop browser.

However, if I open the same page in an android browser (Chrome for example), I noticed that the second ajax function's response is waiting for completion of the first ajax function which kinda defeats the purpose of asynchronous-ness. Both are executing concurrently, but the second function's success is only executing after completion of first ajax call's success function.

Screenshot

The fact that it is working in desktop browsers and not in android browsers leads me to believe that there must be some kind of setting in android which is blocking concurrent asynchronous calls.

If that is the case, is it possible that I can disable that? My code is as follows btw:

$(function(){           
       var intervalID = window.setInterval(function(){
                doAjax(); // this is the function which is waiting for completion of first ajax call
            }, 2000);

      // the first ajax call
      $.ajax({
    type:'post',
    url:'progress-insert.php', // basically is meant for insertion of records into db
    success:function(data)
    {
       clearInterval(intervalID);
    }   
     });

     function doAjax()
     {          
    $.ajax({
        type:'post',
        url:'progress-update.php', // basically returns how many records have been inserted so far
        success:function(data)
        {
                // do something
        }
    }); 
      }

});

解决方案

Unfortunately all browsers implementation of javascript is single-threaded, which means async calls are async for servers but not for same javascript & thus wait for old request. What you can do to overcome that is to push requests in a variable & next time check if request status is not 200 Ok / Success, don't make any further call for same function, sample code can be like:

requests.push($.ajax({
        type:'post',
        url:'progress-update.php', // basically returns how many records have been inserted so far
        success:function(data)
        {
              for(var i = 0; i < requests.length; i++)
              requests[i].abort();
        }
    }));