I am trying to use global variables as flags and cant get it to work. I have two functions:
This function sets a flag to be false when it is done.
function buildYearsTable(btn) { //console.log ("At build years table") var fundCode = document.getElementById("fundCode").value buildYearsFlag = true; $.ajax({url: "/scholarship/scholarshipMaintenance/buildYearsTable", method: "POST", cache: false, data: {fundCode: fundCode}, complete: function(xhr, statusCode){ console.log("inside build years table") data = $.parseJSON(xhr.responseText) $('#myTable tbody').html(''); data = data.sort() data = data.reverse() for(var i = data.length - 1; i >= 0; i--) { moveYearOption(data[i]) addYearRow(data[i]) } buildYearsFlag = false; //$('#yearsTable').html(xhr.responseText) console.log("done in build years table") }}) } This function is called when the first one is called, but i need it to perform its ajax call ONLY once the flag is set to false by the first function. I am not sure how to accomplish this. I was thinking a while loop (polling kind of idea) but not sure how to go about it.
function rebuildYearSelects(btn) { //console.log ("At rebuild selects") var fundCode = document.getElementById("fundCode").value while (buildYearsFlag == false) { $.ajax({url: "/scholarship/scholarshipMaintenance/rebuildYearSelects", method: "POST", cache: false, data: {fundCode: fundCode}, complete: function(xhr, statusCode){ console.log("got inside rebuild years select") data = $.parseJSON(xhr.responseText) selectedYears = data.sortedSelectedYears unselectedYears = data.sortedUnselectedYears $('#yearsModal').replaceWith(data.html) fixModals(); buildYearsFlag = true; console.log("done in rebuildYearSelects") }}) } }