jquery函数返回不起作用

jquery function return doesn't work

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
jQuery: Return data after ajax call success

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//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;
}

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


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

因此,此代码应该有效:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//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
});


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

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


您的closeconnectionsuccess变量超出范围。