javascript - Latency Calculation by AJAX -


i want show connection speed(latency?) of users of website. think can possible measuring xmlhttprequest object request , response times. code follow.

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! result bit different measurement of browser's web console. so, best way ajax or better way ajax? thanks.

if want compare time on how long takes server start responding request, checking against readystate == 2 might work better.

in readystate 2, request has received headers server, should available sooner readystate 4.

also note javascript not accurate on millisecond level may differences because of that.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -