Wait until the Ajax function is done

895 views Asked by At

I have already searched on the site how to wait until the ajax function is done and i found this solution:

function next() {
    $.when(checksign()).done(function () {
        if (uf == 1) {
            $("#usrdiv").css("display", "none");
            $("#pswdiv").css("display", "inline");
        }
    });
}

function checksign() {
    $.ajax({
        url: "checkuser.php",
        data: {
            user: u
        },
        method: "POST"
    }).done(...);
}

but it still not working, it doesn't wait the ajax is done but direcly executes the function inside done()

1

There are 1 answers

1
adeneo On BEST ANSWER

That's because checksign doesn't return the promise you need to chain on a done function.

You don't need the $.when wrapper for one single promise either

function next() {
    checksign().done(function(data) {
        if (uf == 1) {
            $("#usrdiv").css("display", "none");
            $("#pswdiv").css("display", "inline");
        }
    });
}

function checksign() {
    return $.ajax({
        url: "checkuser.php",
        data: {
            user: u
        },
        method: "POST"
    }).done(...);
}