views:

60

answers:

6

Hi,

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 ?

A: 

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.

Jamie Dixon
Yes, that was my little writting mistake. I fixed it. But the whole thing remains the same...
+7  A: 

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.

JacobM
+1 you beat @Darin Dimitrov by 1 second ;)
gnarf
How to fix it: Use jQuery's ajax-option `async: false` http://api.jquery.com/jQuery.ajax/ to wait for the AJAX-Request. Also get rid of the `$(function(){})` document-ready binding.
Marcel J.
@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)
patrick dw
A: 

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.

Darin Dimitrov
+3  A: 

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.

J-P
Neither of these will *execute* immediately -- except if the document is already ready, `$(function(){ ... })` will get called immediately.
gnarf
+1  A: 

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.

SLaks
A: 

$(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.

Marcel J.