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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:02:28+00:00 2026-06-12T11:02:28+00:00

I have a jQuery/JavaSCript function: function CheckPostcode() { // if checkbox is checked then

  • 0

I have a jQuery/JavaSCript function:

function CheckPostcode() {
    // if checkbox is checked then hide and return true
    // if checkbox is not checked (visible or not) then check postcode is known via Ajax call
    // if not known then display the checkbox and return false
    //
    if ($('#perinatalWomanView_AcceptUnknownPostcode').is(':checked')) {
        $("#AcceptUnknownPostcode").hide();
        return true;
    }
    $.getJSON('@Url.Action("PostcodeCheck", "AjaxValidation", new { area = "" })', { postcode: $('#perinatalWomanView_Postcode').val() }, function (result) {
        if (result.postcodeFound == true) {    // executes SECOND
            $("#AcceptUnknownPostcode").hide();
            return true;
        }
        $("#AcceptUnknownPostcode").show();
        return false;
    });
    return false;    // executes FIRST
} //CheckPostcode

that seems to be correct however the AJAX getJSON response is working out of sequence (I used Firebug and alerts separately to check this). I have annotated the code to show the sequence of execution. The Ajax call executes correctly.

What am I missing – thanks.

  • 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-12T11:02:29+00:00Added an answer on June 12, 2026 at 11:02 am

    That’s normal behavior

    This is normal behavior because AJAX is inherently asynchronous, that’s what the A in AJAX stands for.

    This means that requesting stuff from external resources (the server) will be worked in parallel and when results are received you can process them but you can’t count on them worked in a sequence.

    Bad fix

    There is a simple (and bad) fix for sequential minded people: make the AJAX call Synchronous. $.getJSON is equal to $.ajax it just has some predefined parameters; one of the parameters that $.ajax accepts is async which is by default true, replace your $.getJSON call with an equivalent $.ajax call just add the async: false parameter.

    BTW.: jQuery has deprecated this parameter, I’m not all that up-to-date with the current jQuery release but it may already be removed from the API.

    Good easy fix

    Otherwise (recommended) learn about event driven environments. They don’t rely on things being sequential but they provide callback functions to be executed at the moment the event is triggered – This means that the data you need to process is not available at the moment of declaration but at a later moment.

    If you declare a callback for a click/hover/focus event jQuery keeps the callback stored somewhere and only executes it when (and if) the event is triggered. It does not execute it right away and if you don’t click/hover/focus anything it doesn’t execute it at all.

    It’s the same thing with AJAX, you give jQuery a callback function to be run right after the server finished transferring the response.

    BTW.: This fix is not architecturally easy to use everywhere, it works if there’s only one layer of processing over that data, if there are more and logic is involved it’ll get messy.

    Better more complex fix

    This should set you on the straight and narrow, never to stray from it again.

    You can create your own events named whatever you like. Attach an event handler to be executed when that event is triggered.

    The you just tell the AJAX request to trigger that event and pass the event variables along. The event handler you already setup will be called and the rest is poetry.

    $('body').bind('my_event', function(event) {
        results_from_ajax = event.data;
    });
    
    $.getJSON(function(server_results) {
        $('body').trigger('my_event', server_results);
    });
    

    If you look at the trigger function you’ll see that you can define some extra parameters to be sent to the event handlers.

    If you look at the structure of an event object it contains event.type – name of the event, event.target – object that triggered it, event.data – the data that trigger passed on, and many other useful (or not) info.

    Possibly best fix

    There are some newer features that I haven’t read about yet (not in depth) and as far as i’ve understood them they offer an infrastructure for applying operations to some information that is not yet available which operations will be applied sometime after the data becomes available.

    http://api.jquery.com/category/deferred-object/

    I can’t explain this since I haven’t used it and have no understanding of it other than the possibly wrong explanation above. If anyone has the gist of this please enlighten us all.

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

Sidebar

Related Questions

I have a javascript/jQuery block as a callback after $.get function: function myCallBack(data, textStatus)
I have a javascript function generateGraph() { ... jQuery.get('chart.php?month='+month+'&year='+year); document.getElementById('graph').src = 'charts/'+month+'_'+year+'.png'; ... }
I have a JavaScript validation function and a jquery progress bar. onclick of a
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following jquery function > <script type=text/javascript> > > $(document).ready(function() { >
I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =
I have the following Javascript code (using jQuery): floatUpAndDown(); function floatUpAndDown() { $(#bird).animate({top: '+=30px'},
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
Ok, I have this code: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script> <script type=text/javascript> function get()

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.