javascript ajax调用中的jquery退出函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4516167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:58:33  来源:igfitidea点击:

jquery exit function in ajax call

javascriptjquery

提问by Jinbom Heo

Is there a way to exit a function, depending on the result of an GETrequest.

有没有办法根据GET请求的结果退出函数。

For example, in the below function, hi, if the GETresults in data, where data === '1', I want to exit the function.

例如,在下面的函数,hi如果GET结果中data,在那里data === '1',我想退出该功能。

function hi () {
    $.ajax({
        url: "/shop/haveItem",
        type: "GET",
        success: function (data) {
            if (data == '1') {
                // exit hi() function
            }
        }
    });
    // some executable code when data is not '1'
}

How can I go about accomplishing this?

我怎样才能做到这一点?

回答by Arun P Johny

I think the solution can be something like this

我认为解决方案可以是这样的

function hi () {
    $.ajax({
        url: "/shop/haveItem",
        type: "GET",
        success: function (data) {
            if (data == '1') {
                ifData1();
            } else {
                ifDataNot1()
            }
        }
    });
}
function ifData1 () { /* etc */ }
function ifDataNot1 () { /* etc */ }

If you have an ajax function, you should always work with callback functions. If you make an ajax function synchronous, then the browser will be blocked for the duration of ajax call. Which means that the application will remain non-responsive during the duration of the call.

如果您有 ajax 函数,则应始终使用回调函数。如果你让一个ajax函数同步,那么在ajax调用期间浏览器会被阻塞。这意味着应用程序将在通话期间保持无响应。

回答by TimNguyenBSM

You should be able to return false to simulate "exit".

您应该能够返回 false 来模拟“退出”。

function hi()
{
    $.ajax({
            url: "/shop/haveItem",
            type: "GET",
            async:false,
            success: function(data){
                if(data == '1')
                    return false
            }
        });

    //some executable code when data is not '1'
    ...
}

回答by Shamim Hafiz

One thing you can do is have a flag variable. Assign it to true or false, depending on if you want to exit or not.

您可以做的一件事是拥有一个标志变量。将其分配为 true 或 false,具体取决于您是否要退出。

function hi()
{
var flag=false;    
$.ajax({
            url: "/shop/haveItem",
            type: "GET",
            async:false,
            success: function(data){
                if(data == '1')
                    flag=true;
            }
        });
if ( flag ) return;
    //some executable code when data is not '1'
    ...
}

回答by KraigBalla

Creating a global flag variable I think would work the best. Tested and working!!

创建一个全局标志变量我认为效果最好。测试和工作!

window.flag = [];

function hi()
{
    $.ajax({
        url: "/shop/haveItem",
        type: "GET",
        async:false,
        success: function(data){
            if(data == '1')
                flag = true;
        }
    });

    if (flag) {
        return false; 
    }


//some executable code when data is not '1'
...
}