The function below has 2 alert boxes. When I run it, sometimes the outer alert box comes empty. Please explain in a human manner why .
function ItemCount()//returns the number of items in carousel
{
if (window.XMLHttpRequest) {
var req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("upgrade browser");
}
req.open("POST", "/includes/_process/getPortfolioCount.php", true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(null);
req.onreadystatechange = function() {
if ((req.readyState == 4) && (req.status == 200)) {
if (req.responseText.length) {
document.getElementById('portfolioCount').value=req.responseText;
alert(req.responseText);
}
}
}
alert("out:"+document.getElementById('portfolioCount').value);
return document.getElementById('portfolioCount').value;
}//function