7

I think my program is skipping result of JSON call. Is it possible to make a closure function here or make the program wait for JSON call to return?

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "&parameters=" + parameters;
   $.getJSON(url, function(user_name) {
      if (user_name == true) {     
         return true;
      }
   });
   return false;
}
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
bob
  • 71
  • 1
  • 2
  • You want the username_not_duplicate function to wait until the getJSON call finishes? – Pablo Fernandez Aug 05 '10 at 20:49
  • need to see get_functions.php. meanwhile, use firebug to monitor what's being returned to ensure it's a bare boolean, which i doubt, altho you are using the return as if it is – Scott Evernden Aug 05 '10 at 21:01

4 Answers4

13

The $.getJSON() API call is asynchronous. You can make it synchronous by using $.ajax() this way:

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "&parameters=" + parameters;
   var rslt = false;
   $.ajax({
     async: false,
     url: url,
     dataType: "json",
     success: function(data) {
       if (data == true) {     
         rslt = true;
       }
     }
   });
   return rslt;
}
Peter Ivan
  • 1,467
  • 2
  • 14
  • 27
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
  • A key part of this for me was the var rslt = false; I was trying to return true/false from inside the success: which was not returning a bool from the function. – ScubaSteve Mar 12 '13 at 19:15
3

Another choice is to use a call back function and pass it to the function that executes the getJSON as:

//this is the function that executes my getJSON
//callback is the name of my callback function
function getMyData( callback ){

  //here you do your getJSON call
  $.getJSON(url, function(items){

    //do your stuff here

    //call your function here (at the end)
    callback();

  });

}

This way your callback function will be called at the end of the getJSON call.

3

Drew's answer is nearly perfect, just missing one bracket and comma for IE.

function username_not_duplicate(username) {
   var function_name = "get_username"; 
   var parameters = [username]; 
   var url = "camps/includes/get_functions.php?function_name=" + function_name + "&parameters=" + parameters;
   var rslt = false; 
   $.ajax({ 
         async: false, 
         url: url, 
         dataType: "json", 
         success: function(data) {
           if (data == true) {                   
             rslt = true; 
           }
        }, 
    });
    return rslt; 
}
bob
  • 31
  • 1
1

Yeap, username_not_duplicate just returns false immediately because getJSON is asynchronous (ie non-blocking). The return true statement just returns true from the response handler. Normally, you shouldn't do such a blocking calls you're trying to achieve. I suppose you can consider remembering of a state of the request somewhere globally.

eigenein
  • 2,083
  • 3
  • 25
  • 43
  • can i make getJSON non synchronous? – bob Aug 05 '10 at 21:03
  • @bob -- nope. Would be a good option for jQuery to add though. – Drew Wills Aug 05 '10 at 21:06
  • 1
    @bob `$.getJSON` is a simply shorthand of an equivalent `$.ajax` function, which allows to send synchronous requests. See `async` setting at http://api.jquery.com/jQuery.ajax/ . So... the answer is 'yes, not `$.getJSON` but `$.ajax`' – eigenein Aug 05 '10 at 21:09