Verify user account on button click with an AJAX call

Posted on

Problem

I have the following code that works perfectly other than the fact that it has to use async false, to wait for a response from ajax. Is there a way of restructuring this by using callbacks, or deferment?

playbtn.on('mousedown', function () { // functions when clicking play button

    function checkUser() {
        $.ajax({
            url: "/submit/checkuser/",
            async: false,
            success: function (userStatus) {
                if (userStatus == 'Disabled') { // check if user if disabled
                    if (Shadowbox.isOpen()) {
                        Shadowbox.close(); setTimeout(function () { accountDisabled(); }, 750);
                    } else {
                        Shadowbox.clearCache(); accountDisabled();
                    };
                    setTimeout(function () { location.replace('/'); }, 10000);
                } else if (userStatus == 'Deleted') { // check if user if deleted
                    if (Shadowbox.isOpen()) {
                        Shadowbox.close(); setTimeout(function () { accountDeleted(); }, 750);
                    } else {
                        Shadowbox.clearCache(); accountDeleted();
                    };
                    setTimeout(function () { location.replace('/'); }, 10000);
                } else {
                    Shadowbox.setup();
                };
            },
            error: function (e) {
                if (e.status != 403) {
                    if (!Shadowbox.isOpen()) {
                        Shadowbox.clearCache(); connectionError();
                    };
                };
            },
            statusCode: {
                403: function () {
                    if (Shadowbox.isOpen()) {
                        Shadowbox.close(); setTimeout(function () { locationNotAuthorized(); }, 750);
                    } else {
                        Shadowbox.clearCache(); locationNotAuthorized();
                    };
                }
            }
        });
    };

    Shadowbox.clearCache(); // disable shadowbox to check user status
    checkUserTimer = setInterval(function () { checkUser(); }, 600000);
    checkUser();
});

Solution

From: http://api.jquery.com/jquery.ajax/

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

So, it looks like you should do:

$.ajax({ url: "/submit/checkuser" })
 .done(function(userStatus) {
     ... function to complete when done ...
 }).fail(function(e) {
     ... function to complete on error ...
 }).always(function() {
     ... move all your code that needs to be done regardless of error or completion,
     and re-run your timer here
 });

Otherwise, if you are using a version of jQuery that does not include this more-normal looking functionality, there is a “complete” callback available that you can supply just as you are supplying “success” and “error” callbacks, which you can use to do all your cleanup and re-start your timer.

Leave a Reply

Your email address will not be published. Required fields are marked *