0

How can make multiple ajax requests on by one i have a big array of data and i loop through that array and in every loop i need to make ajax request to server but i want to make request only when last request is finsihed

This is my code now:

// This is loop for BigData

length = BigArray.length;

for (i = 0; i < length; i++) {
  token = BigArray[i][0];
  name = titleCase(BigArray[i][1]);
  ajaxRequest(token, name);
}

function ajaxRequest(token, title) {
  $.post(APP_URL + "/message/send", {
    "_token": Laraveltoken,
    title: title,
    token: token
  }, function(data, status) {
    //When Done start next request
  });
}
7
  • Possible duplicate of Sequencing ajax requests Commented Aug 29, 2018 at 14:12
  • 2
    I would strongly suggest you don't make AJAX requests in a loop; you can end up DDOS-ing yourself. Instead loop to congregate all data together then send that in a single request to the server. Commented Aug 29, 2018 at 14:13
  • You can use forEach api.jquery.com/each Commented Aug 29, 2018 at 14:15
  • Either set it up using promises or use a recursive loop. Commented Aug 29, 2018 at 14:20
  • @RoryMcCrossan do you mean just a loop or multiple ajax requests in general? If to call multiple requests using recursive function, may it be the same result you mentioned? Commented May 11, 2020 at 8:44

2 Answers 2

1

I would solve your problem with recursive function.

Steps:

  1. Create a recursive function which will receive one parameter
  2. If array length is larger than 0 continue with the function body
  3. Shift array ( removes the first item from an array and store it into the variable)
  4. Call our function and execute AJAX call with provided parameters, and also pass our array
  5. When AJAX call is finished call our recursive function and pass our array to it

Code:

function myRecursiveFunction(myArray){
   if(myArray.length == 0) return;

   //remove first item of an array then store it into variable item
   var item = myArray.shift(); 
   //call our method which will execute AJAX call to server
   ajaxRequest(item[0], titleCase(item[1]), myArray);
}

function ajaxRequest(token, title, myArray) {
  $.post(APP_URL + "/message/send", {
    "_token": Laraveltoken,
    title: title,
    token: token
  }, function(data, status) {
    //When Done start next request
  }).always(function(){
    //call our recursive function
    myRecursiveFunction(myArray);
   });;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this did the work perfectly :) have a nice day
0

You can use async.js for multiple asynchronous operation.

https://caolan.github.io/async/

Examples:

async.parallel([
    function(callback) { ... },
    function(callback) { ... }
], function(err, results) {
    // optional callback
});

async.series([
    function(callback) { ... },
    function(callback) { ... }
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.