//pings the specified URL, then calls the function result. Result will receive a parameter which is either the time it took to ping the URL, or -1 if something went wrong.
//var PING_TIMEOUT=500; //disabled because it breaks IE11
functionping(url,result){
varxhr=newXMLHttpRequest();
vart=newDate().getTime();
xhr.onload=function(){
if(xhr.responseText.length==0){//we expect an empty response
//try to get more accurate timing using performance API
varp=performance.getEntries();
p=p[p.length-1];
vard=p.responseStart-p.requestStart;
if(d<=0)d=p.duration;
if(d>0&&d<instspd)instspd=d;
}catch(e){
}
result(instspd);
}elseresult(-1);
}.bind(this);
xhr.onerror=function(){
result(-1);
}.bind(this);
xhr.open("GET",url);
/*try{ //disabledbecauseitbreaksIE11
xhr.timeout=PING_TIMEOUT;
}catch(e){}*/
xhr.send();
}
//this function repeatedly pings a server to get a good estimate of the ping. When it's done, it calls the done function without parameters. At the end of the execution, the server will have a new parameter called pingT, which is either the best ping we got from the server or -1 if something went wrong.
varPINGS=3,//up to 3 pings are performed, unless the server is down...
SLOW_THRESHOLD=500;//...or one of the pings is above this threshold