2011-12-26 51 views
0

你好,我有這樣的功能:JavaScript函數真或假

function makeTweet(params) { 
    var tweetID = params.tweetID; 
    var tweetDate = params.tweetUrl; 
    var tweetText = params.tweetText; 
    var tweetUrl = params.tweetUrl; 
    var authorName = params.tweetDate; 
    var authorNickname = params.authorNickname; 
    var authorAvatar = params.authorAvatar; 

    if (haveOauth() == true) { 
     alert("We have the account, make the reply"); 
    }else{ 
     alert("We do not have it"); 
    } 
} 

它應該得到下面的函數的結果,並瞭解它是否返回true或false,並以此爲基礎進行一定的作用。

我該怎麼做?無論我現在做什麼,都會返回相同的結果。 這裏有點不對勁。 感謝

function haveOauth() { 
    var user_id = "1"; 
    var data = $.ajax({ 
     type: "POST", 
     url: "uspolitics_pulse/functions.php", 
     data: { type: 'checkOauth', user_id: user_id }, 
     async: false, 
     success : function(data) { 
      if(data) { 
       return false; 
      } 
     } 
    }); 
} 

回答

1

async: false永遠不會推薦,但你可以嘗試用

var result = false; 
var data = $.ajax({ 
    // ... 
    success : function(data) { 
     result = data; 
    } 
}); 
return result; 

更換

if(data) { 
    return false; 
} 

但是,爲什麼沒有haveOath拿自己的回調

function haveOauth(callback) { 
    var user_id = "1"; 
    var data = $.ajax({ 
     type: "POST", 
     url: "uspolitics_pulse/functions.php", 
     data: { type: 'checkOauth', user_id: user_id }, 
     success : function(data) { 
      callback(data); 
     } 
    }); 
} 

然後

function makeTweet(params) { 
    var tweetID = params.tweetID; 
    var tweetDate = params.tweetUrl; 
    var tweetText = params.tweetText; 
    var tweetUrl = params.tweetUrl; 
    var authorName = params.tweetDate; 
    var authorNickname = params.authorNickname; 
    var authorAvatar = params.authorAvatar; 

    haveOauth(function(data) { 
     if (data) { 
      alert("We have the account, make the reply"); 
     }else{ 
      alert("We do not have it"); 
     } 
    }); 
} 
+0

沒有它下面的函數,那麼r錯誤或真實。不是第一個,第二個功能。 – 2011-12-26 18:24:07

+0

請參閱第二個函數,它說:if(data){ return false; } – 2011-12-26 18:24:39

+0

@DiegoP .:返回將立即發生,您無法返回異步請求的結果值(這可能會導致錯誤)。但是,您可以返回[承諾](http://en.wikipedia.org/wiki/Futures_and_promises)對象。 – Bergi 2011-12-26 19:17:43

0

試試這個:

function haveOauth() { 
    var result = true; 
    var user_id = "1"; 
    var data = $.ajax({ 
     type: "POST", 
     url: "uspolitics_pulse/functions.php", 
     data: { type: 'checkOauth', user_id: user_id }, 
     async: false, 
     success : function(data) { 
      if(data) { 
       result = false; 
      } 
     } 
    }); 

    return result; 
} 
0

邁克爾天鵝的答案相似 - 在這我不認爲你可以永遠到達,你返回true的情況。

function haveOauth() { 
var user_id = "1"; 
var data = $.ajax({ 
    type: "POST", 
    url: "uspolitics_pulse/functions.php", 
    data: { type: 'checkOauth', user_id: user_id }, 
    async: false, 
    success : function(data) { 
     if(data) { 
      return false; 
     } 
    } 
}); 
return true; 
} 
0

功能haveOauth包括不建議是同步的,如@AdamRackis指出AJAX調用。 (除此之外,您return傳遞給$.ajax回調函數內部不會做你想要什麼,你缺少一個return true地方 - 看到@MichaelSwan解決方案)

你可以使用使你的代碼異步獲取傳遞檢查的結果haveOauth回調:

function haveOauth(callback) { 
    var user_id = "1"; 
    $.ajax({ 
     type: "POST", 
     url: "uspolitics_pulse/functions.php", 
     data: { type: 'checkOauth', user_id: user_id }, 
     success : function(data) { 
      if (data) { // or whatever check needs to be made 
       callback(true); // signal that authenticated 
      } else 
       callback(false); // signal that not authenticated 
     } 
    }); 
} 

然後使用像這樣的功能在makeTweet

haveOauth(function(isAuthenticated) { 
    if (isAuthenticated) 
     alert("We have the account, make the reply"); 
    else 
     alert("We do not have it"); 
});