I want to show the connection speed (Latency?) Of the users of my website. I think this is possible by measuring the request of the XMLHttpRequest object and the response time. The code is as follows.
var t;
function ajsend(){
var request = new XMLHttpRequest();
var url;
request.onreadystatechange = function(){
if( request.readyState == 4 && request.status == 200 ){
var u = new Date().valueOf();
var timeTaken = u - t;
var disp;
if( timeTaken < 100 )
disp = "Excellent! " + timeTaken + " milliseconds.";
else if( timeTaken < 500 )
disp = "Very Good! " + timeTaken + " milliseconds.";
else if( timeTaken < 1200 )
disp = "Normal! " + timeTaken + " milliseconds.";
else if( timeTaken < 2000 )
disp = "Poor! " + timeTaken + " milliseconds.";
else
disp = "Very Poor! " + timeTaken + " milliseconds.";
document.getElementById("disp").innerHTML = disp;
}
};
t = new Date().valueOf();
url = "noscript.php";
request.open("GET", url, true);
request.send();
}
It works! But the result is slightly different from the Web Console browser dimension. So is this the best way from AJAX or any better way from AJAX? Thanks.
source
share