简体   繁体   中英

prevent next ajax request wait for previous ajax request

i have 2 ajax requests in 1 page. i want to run that 2 ajax requests together at same time.

First AJAX

$.ajax({
    url: "load1.php",
    type: 'POST',
    success: function (output) {
        console.log(output);
    }
});

Second AJAX

$.ajax({
    url: "load2.php",
    type: 'POST',
    success: function (output) {
        console.log(output);
    }
});

on load1.php i add sleep(10); code, so it will delay 10 seconds.

so while i tried to sleep the first ajax request, the second ajax will wait 10 seconds for the first ajax until its done. i know it can be done by using async:false . but i don't want to use it. is there another way to prevent it and let the second ajax load without waiting the first ajax?

In that case, sleep will not fulfill your requirements. Sleep will delay all of these execution. You can use setTimeout/setInterval for your first ajax request. Then your 2nd ajax request will execute independently & first one will execute after 10 seconds.

Something like this

 setTimeout(function(){ 
    $.ajax({
    url: "load1.php",
    type: 'POST',
    success: function (output) {
     console.log(output);
    }
  });

  }, 10000);

from @G_S statement. it was true that ajax is asynchronous by default. when we use async:false then ajax will become synchronous. i have found my problem that in my php files there was session_start in every files. then when I put sleep code in one of files, it will become session lock. it means that the ajax will wait the session start again to load if there were sleep code..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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