Here is my sample code. (The reason I need to send synchronous requests is that actually I need to send several requests and each requests depends on the previous request's response. And the reason I need to set callbacks is that I want to show some spinner, so users knows the status of the script.)
var xmlhttp =new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==2) {
document.getElementById("p2").style.color = "blue"; //statment 1
}
if (xmlhttp.readyState==4) {
document.getElementById("p2").style.color = "red"; //statment 2
}
}
xmlhttp.open("GET","ajax_info.txt",false);
xmlhttp.send();
balblabla(); // a time-cost function
Then I have 2 questions. First, when will statement 1 and statement 2 actually get executed? Is it guaranteed to get executed before balblabla()?
Second, Even if statement 1 and statement 2 get executed, seems browser won't actually change the displayed color until blablabla() is finished. Is there a way to make color change got displayed before blablabla() finished? (assuming blablabla() takes a long time)
Thanks!