6

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

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

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?

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, a copy mistake I think, I'll remove it
2

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

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'
    ...
}

1 Comment

I think this is one of the best answer. It works and it's simple.
0

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.

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'
    ...
}

2 Comments

Making an ajax call asynch may not be the best solution. It will block the application for the duration of the ajax call
unless async false, falg variable can not catch ajax return value. but i also think I'd better finding another solution
0

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'
...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.