重複の可能性:
jQuery: ajax 呼び出しの成功後にデータを返す
別の関数から呼び出される次の関数があります。関数が機能したかどうかを確認するには、何かを返す必要があります。
//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;
}
関数の成功を返したいのは、実際には最後の行だけです。コードは機能しません。なぜだめですか?