I am still new to javascript and trying to wrap my head around this function I created. I know that javascript executes code asynchronously. However, In the for loop below, I, 1) append my string to a cell in my table 2) if the string is empty or falsey 3) I empty out all the cells and alert the user with a message.
for (var i = 0; i < string.length; i++){
$('<td>').html(string[i]).appendTo(tr);
console.log("These are the params: " + string[i]);
if(!string[i]) {
tbody.empty();
console.log("The string is false returning...");
alert("You have not setup your rack params yet!");
return;
}
}
The problem I am seeing here is, the alert message actually comes before my tbody.empty()
call. So I actually see the previous data table first and after I confirm the alert then it empties out the table.
Can someone explain to me how exactly does the order work here, and how can we fix this to empty the table first then send the alert message?