可能重複:
jQuery: Return data after ajax call successjQuery函數返回不起作用
我有以下函數從另一個函數調用。它需要返回一些信息以查看函數是否工作。
//Function to close any existing connection
function closeConnection(manual) {
//Establish connection to php script
$.ajax({
type: 'POST',
url: 'action/chat/closeconnection.php',
success: function (feedback) {
//If this function was called manually, also empty chatTextDiv and fade out chatDiv
if (manual == true) {
//Stop getting messages
clearInterval(setintervalid);
//Fade out chatDiv
$('#chatDiv').fadeOut(100);
//Empty chatTextDiv
$('#chatTextDiv').html('');
//Fade in openChatDiv
$('#openChatDiv').fadeIn(100);
}
}
}).error(function() {
//Append with connection error - user doesn't know that he is disconnected first so it is better to say this as the function will return false and stop other functions
if (manual != true) {
$('#chatTextDiv').append('You could not be connected. Please try again later.<hr/>');
//Stop getting messages
clearInterval(setintervalid);
var closeconnectionsuccess = false;
}
elseif(manual == true) {
$('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
var closeconnectionsuccess = true;
}
});
return closeconnectionsuccess;
}
它真的只在哪裏我要回功能成功的最後一行:代碼不起作用,那麼。爲什麼不?
總之現在:你不能用一個異步函數返回一個值。 – Blazemonger
您有一個語法錯誤'elseif(manual == true)//「else if」with a space' – elclanrs