Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:35:24+00:00 2026-05-23T23:35:24+00:00

my Ajax calls don’t seem to be asynchronous when wrapped inside a .each loop,

  • 0

my Ajax calls don’t seem to be asynchronous when wrapped inside a .each loop, instead they seem to wait for each to finish before calling the next one ..

MVC C#:

   [HttpPost]
    public JsonResult DoActionGetNextStep(JSONSubmission Submission)
    {
        SomeStruct body = new SomeStruct();
        DateTime start = DateTime.Now;
        try
        {
        System.Threading.Thread.Sleep(5000);
        body.html= "There, done";
        }
        catch (Exception e)
        {
        body.html= "There was an error: "+e.Message;
        }
        TimeSpan s = DateTime.Now - start;
        ast.Html = body.html+ "<p> c# took " +s.Seconds +"s</p>";
        return Json(body);
    }  

JQuery:

function DoActions(targets) {
$(targets).each(function () { DoAction($(this)); });
}

function DoAction(target) {
var parent = $(target).parents('div.actionReplaceable');
var JSONSubmission = { Data : "somedata" };
var Submission = JSON.stringify(JSONSubmission, null, 2);

$.ajax({
    type: 'POST',
    url: '/Main/DoActionGetNextStep',
    data: Submission,
    async: true,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (Result) {
        var html = Result.html;
        $(parent).html(html);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        $(parent).html("JQuery Error: " + textStatus + "\n" + errorThrown);
    }
});
}

This ends up taking 25 seconds for 5 elements, each of them reporting that their call took 5 seconds. I thought ajax calls were asynchronous, so this operation should take 5 seconds total? Both server and browser are running on my localhost. Can anyone spot what I’m missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:35:27+00:00Added an answer on May 23, 2026 at 11:35 pm

    Your requests should be asynchronous. Check with console.log in the appropriate places to see when things happen.

    $(targets).each(DoAction);
    
    function DoAction() {
      var $parent = $(this).parents('div.actionReplaceable'),
          JSONSubmission = { Data : "somedata" };
    
      console.log("sending request for " + this-id);
    
      $.ajax({
        type        : 'POST',
        url         : '/Main/DoActionGetNextStep',
        data        : JSON.stringify(JSONSubmission, null, 2),
        contentType : 'application/json; charset=utf-8',
        success     : function (Result) {
          console.log("received response for " + this-id);
          $parent.html(Result.html);
        },
        error       : function (XMLHttpRequest, textStatus, errorThrown) {
          console.log("received error for " + this-id);
          $parent.html("JQuery Error: " + textStatus + "\n" + errorThrown);
        }
      });
    }
    
    • There is no need for a target parameter. jQuery sets this correctly for callback functions.
    • Once you got rid of the target parameter you just need to pass the function reference to .each().
    • Unless the return type is JSON (seems to be HTML here), setting dataType: 'json' is wrong.
    • setting async: true is superfluous unless you configured the global Ajax options to be async: false. Which I hope you did not.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make some Ajax calls from inside a javascript object.: myObject.prototye = { ajax:
I have nested ajax calls that are not working in ie7. They work fine
I'm trying to make jQuery's ajax calls always return as if they succeeded, e.g.
Should my ajax calls talk to webservices or to simple webpages? It seems to
I am using jQuery to make some ajax calls and wonder how people handle
What's the best practice for making sure that certain ajax calls to certain pages
I've rewritten my family web site using JavaScript (JQuery) making Ajax calls to PHP
I am using jQuery to invoke page methods for my AJAX calls using $.Ajax.
How do I prevent my users from accessing directly pages meant for ajax calls
I used jQuery exclusively on my last project to do all my AJAX calls.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.