Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery exit function in ajax call

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?

like image 634
Jinbom Heo Avatar asked Dec 23 '10 05:12

Jinbom Heo


1 Answers

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.

like image 145
Arun P Johny Avatar answered Oct 13 '22 12:10

Arun P Johny