I have a list of widgets I pull from a database and each one loads in using a switch statement. The problem I have is that it loads out of order if one getJson
call finished before another even if its further down the line. How can I prevent this from happening so they can load in order?
var DashboardArray = DashBoardItems.split(",");
for (index = 0; index < DashboardArray.length; index++) {
switch (DashboardArray[index]) {
case 'totalHours':
$.getJSON(hoursByType, function (json) { hrsByTypeJSON = json; HoursByType(); });
break;
case 'invoiceList':
InvoiceListNav();
break;
case 'prevInvoiceWidget':
PreviousInvoice();
break;
case 'payrollSchWidget':
PayrollSchedule();
break;
case 'empListWidget':
EmployeeList();
break;
case 'executiveOverview':
$.getJSON(execOverview, function (json) { execOverJSON = json; ExecOverView(); });
break;
case 'grossWagesDept':
$.getJSON(dashboardURL, function (json) { gwdJSON = json; WagesByDept(); });
break;
case 'grosswagesbyTrend':
$.getJSON(wagesByTrend, function (json) { wageTrendJSON = json; grosswagesbyTrend();});
break;
case 'wagesPos':
$.getJSON(wagesByPosition, function (json) { posJSON = json; WagesByPos(); });
break;
case 'totalreghoursbyTrend':
$.getJSON(RegHrsTrend, function (json1) {
RegHrTrendJSON = json1;
$.getJSON(OTHrsTrend, function (json2) {
OTHrTrendJSON = json2;
$.getJSON(PTOHrsTrend, function (json3) {
PTOHrTrendJSON = json3;
$.getJSON(OtherHrsTrend, function (json4) {
OtherHrTrendJSON = json4;
totalRegHoursTrend();
});
});
});
});
break;
case 'employeeByType':
empTypePie();
break;
case 'totalInvoice':
$.getJSON(totalInvoice, function (json) { TTLInvJSON = json; TotalInvoice(); });
break;
case 'totalInvoiceDept':
$.getJSON(InvoiceByDiv, function (json) { InvDivJSON = json; TotalInvoiceDept(); });
break;
case 'totalinvoicebyTrend':
$.getJSON(InvoiceTrend, function (json) { InvTrendJSON = json; totalInvoiceTrend(); });
break;
}