In the following code, is doThisFirst()
guaranteed to execute before doThisSecond()
?
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() :
new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
doThisSecond(xmlhttp.responseText);
}
};
xmlhttp.open("GET", "/myscript.php?", true);
xmlhttp.send();
doThisFirst();
If not, how can I guarantee this execution order (aside from the naive solution of setting a flag at the end of doThisFirst()
and then polling while(!flag);
before executing doThisSecond()
)?. I'm looking for a pure-Javascript solution (no jquery).
Important note: I do not want to execute the AJAX request synchronously, which would execute doThisSecond()
before doThisFirst()
.