0

Hello all i am having a problem with my Jquery function. i have a variable called "var checkBit" I perform a $ajax action and based on that i set the "checkBit" to either 1 or 0. But when i alert the "checkBit" it always shows 0 even when i have reassigned it the value 1. I have commented on the code where i have the problem. Here is my code:

function checkStatusType(statusId){
    var checkBit = 0; //deceleration of variable and initially assigned 0
    $.ajax({ //ajax action performed
        url:'ajax_1.php',
        type:'GET',
        data:{task:'check_status_type',id:statusId},
        dataType: 'json',
        cache:false,
        success:function(data){
            if(data.length > 0){

                if(data != 0){ //here i am checking if the value of "data" //is 1 or not
                    $('#expected-return-time').slideDown();
                    checkBit = 1;  //based on the above check i assign 
                    //either 1 or 0 to the variable

                    //alert(checkBit); when i alert it here it works fine
                } else {

                    $('#expected-return-time').remove();
                    checkBit = 0;  // same here 
                    //alert(checkBit);

                }
            } else {
                alert('Sorry unable to find the status ID');
            }


        },error: function(data) {
            console.log('Error: ' + data);
        }

    });
    alert(checkBit); //problem when i alert the variable here it always 
    //shows 0 only every time. I dont know whats wrong with it.

}

My basic objective was that my function will return the variable and based on value 1 or 0 i have different functions that i perform. if there more effective way of doing i did like to learn. Thank you very much.

Sparko
  • 735
  • 6
  • 15
Chakra
  • 31
  • 2
  • 8
  • just alert the data and show us – GautamD31 Jun 11 '13 at 06:40
  • 2
    it is because ajax is asynchronous means once the request is sent to the server before the response comes back the `alert(checkBit)` will get executed – Arun P Johny Jun 11 '13 at 06:41
  • The solution in this case is to move all code that depends on the ajax response to the success callback of the ajax request – Arun P Johny Jun 11 '13 at 06:42
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – JJJ Jun 11 '13 at 06:43
  • Try using `async: false` in your ajax code – Prashant16 Jun 11 '13 at 06:45
  • 1
    @Prashant16 That's completely unnecessary and bad practice altogether. – JJJ Jun 11 '13 at 06:45
  • Ok guys i will try it out. @prashant way is working but i will also try other ways. thanks guys for input, u guys help newbies like us to grow :) – Chakra Jun 11 '13 at 06:51

2 Answers2

0

Ajax query is asynchronous and it don't wait for finish and execute alert(checkBit);

Use smt like this:

$.ajax({ //ajax action performed
url:'ajax_1.php',
type:'GET',
data:{task:'check_status_type',id:statusId},
dataType: 'json',
cache:false,
success:function(data){
      finish();
}});

function finish() {
    alert(checkBit);
}
jtomaszk
  • 9,223
  • 2
  • 28
  • 40
0

The problem with the final call to:

alert(checkBit);

Is that this occurs before the response from the ajax has completed, and hence will display 0 every time. I'd recommend passing a callback function into checkStatusType to encapsulate the processing you'd like to do with the returned checkbit.

Sparko
  • 735
  • 6
  • 15