2

Can someone one explain it please?

  1. Why alert 2 pops before alert 1?
  2. Why value of pageCount in alert 1 is different than alert 2?
function naviSet()
{
    var pageCount;
    if($.ajax({
        type: "POST",
        url: "http://localhost/mywebsite/wp-content/themes/twentyeleven/more-projects.php",
        success:function(data)
        {
            pageCount = data;
            alert(pageCount); //alert 1
            return true;
        },
        error:function()
        {
            $("#direction").html("Unable to load projects").show();
            return false;
        }
    })) alert(pageCount); //alert 2
}
1
  • coz its asnc call kaleem ..:) Commented Mar 27, 2014 at 11:45

5 Answers 5

2

The alert1 is inside a callback - this function will only be called when the ajax request completes successfully (ie asynchronously).

The pageCount is different for the same reason - the success callback has not been made when alert2 is called.

Sign up to request clarification or add additional context in comments.

Comments

2

As most answers mention you make an asynchronous call, but thats not really the reason. So JavaScript is single threaded, only on think can be done per time.

So first you call your function and this function will put on the execution context stack. This function will be executed before any other function that will be added to stack can be executed. In this function you make your ajax call and on success the success function will be put on the execution context stack. So this function could never ever called before naviSet. As alert1 is made in the naviSet function it will be the called first.

And to your second question:

From your function I think you believe, when $.ajax() returns true, your ajax call was succesful and pageCount was set to data. But it isn't. $.ajax doesn't return true but the truethy value $. Its a function that return reference to the main jquery object, so you can chain function calls.

function naviSet()

{
    //you create a new var which is undefined
    var pageCount;
    // return $ which is a truethy in JavaScript, but it does not mean the ajax call was successful
    if($.ajax({
        type: "POST",
        url: "http://localhost/mywebsite/wp-content/themes/twentyeleven/more-projects.php",
        success:function(data)
        {
            // now you in the context of your success function 
            // and set the value of your   variable to data
            pageCount = data;
            alert(pageCount); //alert 1
            return true;
        },
        error:function()
        {
            $("#direction").html("Unable to load projects").show();
            return false;
        }

    })) 
    //here you are still in the context of your naviSet function where pageCount is undefined
    alert(pageCount); //alert 2
}

Comments

1

Why alert 2 pops before alert 1?

Alert 1 is fired by the callback function that is fired when a successful HTTP response has been received.

Alert 2 fires as soon as the HTTP request has been sent.

Networks are slow.

Why value of pageCount in alert 1 is different than alert 2?

Because it is changed when the response has been received (just before it is alerted), by the same callback function as mentioned above.

1 Comment

It has nothing to do with slow networks, but with the single thread nature of JavaScript. Event with zero network time it can't be called before alert2.
1

The ajax-function retrieves data from the given url asynchronously. That means that it is doing it in the background, while the rest of your code executes. As soon as it is finished, the function assigned to "success" is called (or "error", if it fails).

The second alert is called first because of this. Like I said, the rest of the code continues execution while the ajax-function is working.

Comments

1

The reason the second alert happens first is because the ajax call is asynchronous. It essentially schedules a web call and returns immediately. Hence the line after it which is the second alert happens directly after.

At some point later in time the web request will complete and call into the success function. Hence the first alert happens at that point

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.