如何退出这个功能结构?

时间:2011-04-06 05:56:04

标签: javascript jquery

function myfunction() {
    ...
    $(myarray).each(function() {
        ...
        $.ajax({
            ...,
            async: false,
            error: function() {
                // I want to return false and quit from this function here
                // no luck with return false, still continuing to the next loop
            }
        });
    };
}

1 个答案:

答案 0 :(得分:2)

使用变量:

function myfunction() {
    var gotError=false;
    $(myarray).each(function() {
        if(gotError) return;
        $.ajax({
            ...,
            async: false,
            error: function() {
                gotError=true;
            }
        });
    };
    return !gotError;
}