Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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();
});
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 ...
 }).error(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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.