javascript - FB.api() method prevents returning true or false -


i'm breaking brains 1,5 hour on little problem i've got. situation: file -> request file b, if file b returns true, user registered , doesn't need added our database. if false, user signed facebook, never logged in before -> add database.

my problem: fb.api() prevents function returning true because of async-call.

function registered(){                 fb.api('/me', function(response) {                     $.ajax({                     async: true,                     url: "facebook-sdk/src/check.php",                     type: "post",                     data: {id: response.id},                     success: function(data) {                         if(data=='true'){                             return true;                         }else{                             return false;                         }                     }                     });                 });                     } 

and call function:

 if(registered())     alert('true');  else     register(); 

you change like:

function registered(callback){ //pass callback function parameter    fb.api('/me', function(response) {        $.ajax({           async: true,           url: "facebook-sdk/src/check.php",           type: "post",           data: {id: response.id},           success: function(data) {               callback(data);           }        });     });                     } //call registered function registered(function(response) { //callback function here     if(response) { //if true returned       alert("registered already");     }     else {       your_register_function();     } }); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -