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 8566031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:33:49+00:00 2026-06-11T17:33:49+00:00

I had read many articles about that async is not user friendly but in

  • 0

I had read many articles about that async is not user friendly but in the following procedure everything messed up when I remove async because it needs one by one execution.

for (var k = 0; k < selectedValueArr.length - 1; k++) {
    var value = selectedValueArr[k];
    $.ajax({
        type: "POST",
        url: dm + "Services/AjaxService.asmx/GetCityCheck",
        dataType: "json",
        data: JSON.stringify({ code: value }),
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function(data) {
            html += '<li style="height: 25px;"><div style="font-weight: bold; background-color: #91c8e2; padding: 3px 3px; font-size: 13px;">' + document.getElementById('stateName' + selectedValueArr[k]).innerHTML + '</div></li>';
            var datafromServer = jQuery.parseJSON(data.d.toString());
            $.each(datafromServer, function(key, value) {
                html += '<li style="height: 18px; font-size: 12px;"><span pvalue="' + key + '"><input id="chkCity' + key + '" type="checkbox" title="' + value + '" style="border: 0px;" onchange="javascript:CityCheck(this)"><span title="' + value + '" id="cityName' + key + '">' + value + '</span></span></li>';
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
} 

Can anyone tell me what is the replacement for async because it really hangs the browser…

  • 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-06-11T17:33:50+00:00Added an answer on June 11, 2026 at 5:33 pm

    Here’s an advice: instead of hammering your server with many AJAX requests in a loop it’s better to send all the data in a single AJAX request even if more data will transmit through the wire. It’s always better to minimize server roundtrips.

    So get rid of the for loop and then send a single AJAX request:

    $.ajax({
        type: 'POST',
        url: dm + 'Services/AjaxService.asmx/GetCityCheck',
        data: JSON.stringify({ codes: selectedValueArr }),
        contentType: 'application/json; charset=utf-8',
        success: function(data) {
            // work with data.d directly here, don't do any $.parseJSON
            // see below for how you need to modify your WebMethod for
            // this to work
            ...
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
    

    and then modify your WebMethod so that it takes an array as input parameter and returns an array as output:

    [WebMethod]
    public SomeModel[] GetCityCheck(string[] codes)
    {
        ...    
    }
    

    Notice that if your WebMethod returns directly a model instead of a string you don’t need to do any jQuery.parseJSON inside your success callback but directly work with data.d.

    And if you don’t want to follow my advice and kill your server with AJAX requests in a loop you will have to capture the k indexer variable in a closure:

    for (var k = 0; k < selectedValueArr.length - 1; k++) {
        (function(i) {
            var value = selectedValueArr[i];
            $.ajax({
                type: "POST",
                url: dm + "Services/AjaxService.asmx/GetCityCheck",
                dataType: "json",
                data: JSON.stringify({ code: value }),
                contentType: "application/json; charset=utf-8",
                context: value,
                success: function(data) {
                    html += '<li style="height: 25px;"><div style="font-weight: bold; background-color: #91c8e2; padding: 3px 3px; font-size: 13px;">' + document.getElementById('stateName' + this).innerHTML + '</div></li>';
                    var datafromServer = jQuery.parseJSON(data.d.toString());
                    $.each(datafromServer, function(key, value) {
                        html += '<li style="height: 18px; font-size: 12px;"><span pvalue="' + key + '"><input id="chkCity' + key + '" type="checkbox" title="' + value + '" style="border: 0px;" onchange="javascript:CityCheck(this)"><span title="' + value + '" id="cityName' + key + '">' + value + '</span></span></li>';
                    });
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        })(k);
    }
    

    Also notice how I am using the context parameter to the AJAX request in order to pass the value to the success callback. Then inside this success callback I have replaced the selectedValue[k] with this because the current context is now different. This is the proper way to pass information to the success callback from the outer context of the $.ajax call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had read somewhere about one specific feature that is present in awk but
I've read many articles and several post (including here in stackoverflow) but do not
I had read many articles regarding skype. According to them, Skype is a pure
I had read many database design books, but all of them use only one
i had read the documentation of python and many sources about the meaning of
I was fairly sure that I had read (in Richter's C# book) that objects
Had a good read through similar topics but I can't quite a) find one
I read about Big-O Notation from here and had few questions on calculating the
I recently read in a presentation on Scribd that Facebook had benchmarked a variety
I've read somewhere that the trunk version of Firefox already had a WebSocket implementation.

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.