1

a little misunderstanding with JQuery and JS variables. Simple example:

function someFunc(){

   var flag = true;

    $(function(){
        $.post("/some/address/", {} ,function(data){
            if( data == false){             
                flag = false;
            }
        });
    });
   if(flag == false){
      return false;
   }    
} 

The main problem is accessibility of variable flag in Jquery functions. I always get the same flag value equal to true. How to make this done ? How to make global variable to be visible for JS and JQuery ?

2
  • 8 answers after 6 minutes... wow Commented Jul 1, 2010 at 14:42
  • This question is similar to: How do I return the response from an asynchronous call?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 12, 2024 at 8:50

6 Answers 6

6

I don't think your problem here is one of scope, actually; I think your problem is that the post function is asynchronous. That is, it goes off to the server to get data, and runs the callback function (which updates your flag) when the data comes back (which might take a while). But meanwhile, the surrounding function keeps on going; it doesn't wait for the post to return. So the flag update happens after the outer function has returned.

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

2 Comments

How to fix it: Use jQuery's ajax-option async: false api.jquery.com/jQuery.ajax to wait for the AJAX-Request. Also get rid of the $(function(){}) document-ready binding.
@Marcel - I wouldn't recommend async: false without noting that it will lock up the browser until the response is received, which could be an issue. There are other remedies that aren't so potentially disruptive. If having the browser locked is acceptable, then async: false is a good solution. :o)
3

You're doing two asynchronous operations:

  • $(function(){}) -- fires function when document is "ready"
  • $.post -- fires XHR request

Neither of these will return immediately, -- e.g. an XHR request takes time to complete -- this is why you're passing a callback function (so that you can do stuff upon completion).

While these operations are occurring your JS will carry on executing, which means that this:

if(flag == false){
   return false;
}

... is executed before either operation (at best, just the $.post operation) completes.

1 Comment

Neither of these will execute immediately -- except if the document is already ready, $(function(){ ... }) will get called immediately.
0

The AJAX callback only executes after you return.

Therefore, when you write if(flag == false), the post callback hasn't executed yet, so flag is still true.

Comments

-1

At a first glance it appears that by setting flag = 'false' (with 'false' being a string) instead of flag = false as a bool, that testing for flag == false will never be true.

UPDATE

Also, your jQuery code is doing a post which will return much later than the check for flag == false. It seems you're asuming that the code is operting entirely in a linear fashion however the callback method you're setting flag == false in, will be called at a much later time.

1 Comment

Yes, that was my little writting mistake. I fixed it. But the whole thing remains the same...
-1

The problem is that the callback function is called much later as AJAX is asynchronous. The $.post returns immediately and the next line executes when flag is still false.

Comments

-1

$(function(){ ... }) and $.post("address", function() { ... }) do not block. So your callback code at ... won't be executed before the js-interpreter hits if(flag == false). It's equivalent to:

var flag = true;
doSomethingLater();
if(flag == false)
  ...

flag will always be true.

Remove $(function(){ ... }) and use the async: false option for ajax requests.

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.