$.done() code gets executed before $.when()

216 views Asked by At

In my project I m using 2 versions of JQuery.js, so I resolved the conflict using $.noConflict() for my latest version of JQuery to variable named jq172. Now I have used jq172.when().done()construct to show loading status image until all ajax request are processed completely. The code is as follows.

jq172.when(
    DisplayPOXOrderStatistics(fromDate, toDate), 
    DisplayPOXCompletedOrdersData(fromDate, toDate),
    DisplayPOXCompletedOrderPieChart(fromDate, toDate),
    DisplayCurrentYearlyTrends())
      .done(function (a1,a2,a3,a4) 
        { 
           $("#loading").hide(); 
        });

where functions in the jq172.when() are coded as follows,

 function DisplayPOXOrderStatistics(fromDate, toDate) {        
    return $.ajax({
        type: "POST",
        url: '@Url.Action("DisplayPOXOrderStatistics", "Home")',
        dataType: "json",
        data: { FromDate: fromDate, ToDate: toDate },
        success: function (data) {application code.....}
    });        
}
function DisplayPOXCompletedOrdersData(fromDate, toDate) {
    return $.ajax({
        type: "POST",
        url: '@Url.Action("DisplayPOXCompletedOrders", "Home")',
        data: { FromDate: fromDate, ToDate: toDate },
        dataType: "json",
        success: function (data) { some code....}
    });
    }

& rest 2 functions are coded in the same way as above Now my code in .done() to hide loading image dive should be executed after all 4 ajax call is finished, but currently it gets executed immediately after function call is dispatched. Can anybody figure out the problem in my code. Thanks in Advance...

Here is the definition of rest 2 functions..

function DisplayPOXCompletedOrderPieChart(fromDate, toDate) {
    return $.ajax({
        type: "POST",
        url: '@Url.Action("POXCompletedOrderPieChart", "Home")',
        data: { FromDate: fromDate, ToDate: toDate },
        dataType: "json",
        success: function (data) {
            $('#POXCompletedOrdersPie').empty();
            var dataSet = [];
            var isDataAvailable = false;
            for (var i = 0; i < data.length ; i++) {
                var newElement = { label: data[i].Name, data: parseFloat(data[i].ColumnValue), color: Color[i] };
                dataSet.push(newElement);
                if (newElement.data > 0)
                    isDataAvailable = true;
            }
            if (dataSet.length != 0 && isDataAvailable) {
                $.plot($("#POXCompletedOrdersPie"), dataSet, {
                    series: {
                        pie: {
                            show: true
                        }
                    },
                    legend: {
                        show: false
                    }
                });
            }
            else {
                $("#POXCompletedOrdersPie").empty();
                $("#POXCompletedOrdersPie").append("<html><p> <b>" + "@Html.Raw(VirtuOxAdvDME.Dashboard_PieChart_POXCompletedOrder_NoData)" + "</b> </p><html>");
            }
        }
    });        
}

function DisplayCurrentYearlyTrends() {
    $("#DisplayCurrentYear").html($('#selectCurrentYear option:selected').text());
    return $.ajax({
        url: '@Url.Action("DisplayCurrentYearlyTrends", "Home")',
        data: { selectedCurrentYear: $('#selectCurrentYear option:selected').text() },
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            var DataValues = [], TickData = [];
            var i = undefined;
            $.each(data, function (index, item) {
                i = (index + 1) * 2;
                DataValues.push({ data: [i, item.Value], color: Color[i] });
                DataValues.push([i, item.Value]);
                TickData.push([i, item.MonthName]);
            });
            $.plot($("#CurrentYearlyTrendsBar"), [{ data: DataValues, color: "#3D69AA" }],
                    {
                        series: { bars: { show: true } },
                        bars: {
                            barWidth: 1.5,
                            align: "center"
                        },
                        xaxis: {
                            ticks: TickData,
                            axisLabelUseCanvas: true,
                            labelAngle: -90,
                        },
                        yaxis: { axisLabelUseCanvas: true },
                        grid: { hoverable: true }
                    });
            $("#CurrentYearlyTrendsBar").UseTooltip();
        }
    });        
}
2

There are 2 answers

3
Bergi On BEST ANSWER

Probably your $.ajax calls (from the old jQuery version) do not return jqXHR objects which implement the Promise interface, a feature that was introduced with v1.5. when would see the objects as plain values then and resolve immediately.

To fix this, use jq172.ajax() instead, or use a (single) up-to-date jQuery and update your legacy code.

3
Parv Sharma On

this is from the jquery site

in the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

check your ajax calls and make sure no one is getting rejected.
You can do this in the developers console->network tab
generally becomes accessible after pressing F12.