I'm writing a little web-app and want to check the users connectivity before I allow him to submit his data.
function testConnection() {
var xmlhttp;
document.getElementById("checkingConnectivity").style.display = "block";
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var success = false;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("noConnectivity").style.display = "none";
document.getElementById("checkingConnectivity").style.display = "none";
success = true;
}
}
connIterator = connIterator + 1;
xmlhttp.open("GET","testConnection.jsp?i="+connIterator,true); // Dynamic URL to prevent caching
xmlhttp.send();
// Wait 10 seconds
for(var i = 0; i < 10; i++) {
// If no success so far, keep waiting
if(!success) {
window.setTimeout('1000');
} else {
return true;
}
}
// success still isn't true, so we assume a timeout
document.getElementById("noConnectivity").style.display = "block";
document.getElementById("checkingConnectivity").style.display = "none";
return false;
}
The problem is that this function always returns false, even if the file is reachable.
When adding an alert before window.setTimeout('1000');
it works, so I assume the the setTimeout
does not work.
Do you have any suggestions?
Thanks in advance!