0

I have spent the last 30 mins figuring this out.

Let's assume the following case:

<script>
    var x = 0 ; //global variable
    function compute() {
        x = x+1; 
    }
</script>

Should the value of x be incremented so I can access it into a function later on?

I have the following codes:

var id1 = 0;
var id2 = 0; 
$.ajax({
    url: "http://localhost/twee/chat/comment",
    type: 'POST',
    data: form_data,
    success: function(data) {
        var data1 = parseInt(data);
        id1 = id2 = data1; 
    }
});

I want to be able to access the values of id1 and id2 (updated since i declared them as 0) in a later function.

However, the value is staying zero and data1 is retrieved just fine.

4
  • Don't call parseInt without specifying the base with the second argument. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… Commented Jun 12, 2012 at 17:36
  • I se you declare id1 and id3 then you ask for id2 ? undefined exception will be thrown Commented Jun 12, 2012 at 17:38
  • The data is retrieved just fine. The problem is not with parseInt. Commented Jun 12, 2012 at 17:39
  • @TrinhHoangNhu I saw and corrected that. Sorry Commented Jun 12, 2012 at 17:39

3 Answers 3

2

I dont know when you try to use the ID's, but the ajax call is asynchronus, so if you try to read the variables like this:

var id1 = 0;
var id3 = 0 ; 
$.ajax({
         url: "http://localhost/twee/chat/comment",
         type: 'POST',
         data: form_data,
         success: function(data) {
              var data1 = parseInt(data, 10);
          id1 = id2 = data1;

         alert(id1) // Will alert whatever number returned as data. 
}});

alert(id1) // Will alert 0

The second alert will be called almost simultaneously with the ajax call, and at that point the data has not been updated yet, while the first alert will not be called until the data has returned. If you are going to use the ID's, you will have to use them within your success-callback, or in a function that is called within your success-callback.

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

7 Comments

Okay can i not assign it to a global variable and use it?
@DpkgMe Well, you can assign it to a global variable, but if you try to read that global variable outside the success-callback, it is likely that the variable hasn't been set yet, because the data hasn't returned at that point. That is why you use the success-callback, so that you know that you have the data, prior to using it.
The above seems likely, but it assumes id1 and id2 are in the same scope as whatever functions try to call them. From this snippet they appear to be global. If this code exists in some kind of wrapper function, though, you'll need to make sure the calling routines are in the same scope.
Ok i will just go with my plan B instead. I am using this with pusher and will send the ids within the pusher trigger. Thanks everyone.
@jatrim Thanks for the clarification, I just assumed that they were not wrapped by another function, as OP stated that they are gobal.
|
0

Are you sure that you're checking the value of id1 after the success callback runs? ajax is an asynchronous operation, your callback won't be called until the response comes back from the server.

Comments

0

Your ajax callback is asynchronous. It won't run the success function until after the remote server responds. Once it does, your function is executed, and the variable is incremented.

This is a difficulty in Javascript programming, and asynchronous programming in general: there are a number of balls in the air at once, and it is difficult to reason about how they might complete their operations, and when you can see the results.

It's very easy to think of the $.ajax() call as "get the results from the server," but it's really, "make a request to the server." Who knows when it might finish?

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.