jquery函数返回不起作用

时间:2012-05-09 20:12:33

标签: jquery function return

  

可能重复:
  jQuery: Return data after ajax call success

我有以下从另一个函数调用的函数。它需要返回一些东西才能看出函数是否有效。

//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;
}

这真的只是关于我想要返回函数成功的最后一行:代码不起作用。为什么不呢?

3 个答案:

答案 0 :(得分:3)

正如@Rockitsauce所说,你的closeconnectionsuccess变量不在函数范围内,因此在那时无法访问。但更重要的是,您必须考虑到您正在使用jQuery的异步函数 - 函数closeConnection将在调用回调(successerror)之前完成执行。如果要在HTTP请求完成后检索任何结果,您还必须向自己的函数添加回调函数,从而使其也异步。

因此,此代码应该有效:

//Function to close any existing connection 
function closeConnection(manual, callback) {
//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);
        callback(false);
    }
    elseif(manual==true)
    {
        $('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
        callback(true);
    }
});
}

closeConnection(manual, function(success) {
    //Process result
});

答案 1 :(得分:1)

您的closeconnectionsuccess变量超出范围。

答案 2 :(得分:1)

ajax调用是异步的。因此函数在ajax调用返回之前到达结尾。

您必须传入并调用回调函数或在ajax调用完成时触发事件。