0

I am doing the following through jQuery, but it looks like they are almost happening simultaneously.

Is there a way to make sure itemNameContainer.removeClass('NoDisplay') in the complete callback to run only after the loadingContainer.addClass('NoDisplay') is completed?

Visually, it looks like I am seeing 'Please wait..' and the item name show up at the same time..

function onToggleItemCompletionStatus(currentItem) {
    var itemId, toggle = !currentItem.Completed,
        loadingContainer, itemNameContainer;
    itemId = currentItem.ItemId;
    loadingContainer = $('#loading_' + itemId);
    itemNameContainer = $('#name_' + itemId);

    $.ajax({
        beforeSend: function (xhr, settings) {
            loadingContainer.removeClass('noDisplay');
            itemNameContainer.addClass('noDisplay');
        },
        complete: function (xhr, textStatus) {
            loadingContainer.addClass('noDisplay');
            itemNameContainer.removeClass('noDisplay');
        },
        data: {
            accessToken: aToken,
            listId: currentGroceryList.Id,
            itemId: currentItem.ItemId,
            completed: toggle
        },
        dateType: 'json',
        error: function (xhr, textStatus, errorThrown) {
            $.publish(customEvent.ItemToggledFail, [currentItem]);
        },
        success: function (data, textStatus, xhr) {
            var success = data.success;

            if (success) {
                $.publish(customEvent.ItemToggledSuccess, [currentItem]);
            } else {
                $.publish(customEvent.ItemToggledFail, [currentItem]);
            }
        },
        type: 'POST',
        url: actionUrls.toggleItemCompletionStatus
    });
}

EDIT I pasted the actual function to give a better idea

3
  • Nothing is jumping out at me...is the request synchronous or asynchronous? Can you post the rest of the $.ajax call? Is the response just arriving very quickly? Commented Jan 28, 2011 at 0:20
  • They should be performed in a matter of microseconds, so a callback isn't given. There is no after CSS applied hook that I know of. You could be hacky with setTimeout(), but I often don't recommend it. Commented Jan 28, 2011 at 0:20
  • I guess everything is just happening in a matter of microseconds.. Commented Jan 28, 2011 at 0:43

1 Answer 1

2

Not that I know of, but you could try...

loadContainer.fadeOut(300, function() {
   itemNameContainer.removeClass('NoDisplay');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I think this approach makes the transition appear smoother. It's not perfect, but everything is happening in a matter of microseconds anyway.

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.