function contents execution not in order

Total Post:183


 1242  View(s)
Rate this:
Hi Expert!

I have a javascript function as below

function myfunction() {

    var url = location.href;
    var ajaxRespose;

            url: url,
            dataType: "text",
            success: function(response) {
                var data = $.parseJSON(response);
                ajaxRespose = data;
                console.debug("ajaxRespose ==>:"+ajaxRespose);
        console.debug("first ajaxRespose: " +ajaxRespose);
    return false;
on my console (firbug) i get :

first ajaxRespose: undefined

ajaxRespose ==>:[object Object]
My question is, why the ajax call execute after the "first" console.debug. PS: i have simplified the function, (function works ok, but problem is in 

sequence of execution)

Thanks in advance!
  1. Re: function contents execution not in order

    Hi Ankita!

    Because $.ajax() is asynchronous, the sequence of events happen like this:

    $.ajax(...);   // executes AJAX request
    console.debug("first ajaxResponse:" + ajaxRespose);   // undefined

    /* some time later, we receive AJAX response */

    // it executes the success function from the original AJAX request
    console.debug("ajaxRespose ==>:"+ajaxRespose);  // [object Object]


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.