Of course, the length of HTTP headers cannot be counted at request, so when the request packet length is at the maximum Transmission unit critical point, the additional HTTP headers can cause IP groupings, so there is a certain error. (2009/6/21)
Online Demo: http://demo.jb51.net/js/2011/ping/
Core code:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>http ping</title>
<style>
Html
{
height:100%;
Overflow:hidden;
}
Body
{
Background: #000;
Color: #C0C0C0;
Font-weight:bold;
font-size:14px;
Font-family:lucida Console;
height:100%;
margin:0 0 0 5px;
}
#divContent
{
height:90%;
Overflow:auto;
}
#txtTimeout
{
width:40px;
}
button
{
margin-left:10px;
}
</style>
<body>
<div id= "Divinput" >
<span>URL:</span>
<input id= "Txturl" type= "text"/>
<span>Timeout:</span>
<input id= "Txttimeout" type= "text" value= "to"/>
<input id= "Btnswitch" type= "button" value= "Start" onclick= "Handlebtnclick ()"/>
</div>
<div id= "Divcontent" ></div>
<script>
var intstarttime;
var objimg = new Image ();
Objimg.onload =
Objimg.onerror =
function ()
{
/*
* There is a response, cancel timeout timing
*/
Cleartimeout (Inttimerid);
if (!bolisrunning | | bolistimeout)
Return
var delay = new Date ()-intstarttime;
println ("Reply from" +
strURL +
"Time" +
((delay<1)? ("<1"):("=" +delay)) +
"MS");
Arrdelays.push (delay);
/*
* Each request interval is limited to more than 1 seconds
*/
settimeout (ping, delay<1000?) ( 1000-delay): 1000);
}
function ping ()
{
/*
* Send Request
*/
Intstarttime = +new Date ();
intsent++;
OBJIMG.SRC = strURL + "/" + intstarttime;
Bolistimeout = false;
/*
* Timeout Timing
*/
Inttimerid = settimeout (timeout, inttimeout);
}
function timeout ()
{
if (!bolisrunning)
Return
Bolistimeout = true;
OBJIMG.SRC = "x:\\";
println ("Request timed out.");
Ping ();
}
</script>
<script>
var $ = function (v) {return document.getElementById (v)};
var arrdelays = [];
var intsent;
var bolisrunning = false;
var bolistimeout;
var strURL;
var inttimeout;
var Inttimerid;
var objbtn = $ ("Btnswitch");
var objcontent = $ ("divcontent");
var Objtxturl = $ ("Txturl");
Objtxturl.value = Window.location.host;
function Handlebtnclick ()
{
if (bolisrunning)
{
/*
* Stop
*/
var intrecv = arrdelays.length;
var intlost = intsent-intrecv;
var sum = 0;
for (var i=0; i<intrecv; i++)
Sum + + arrdelays[i];
Objbtn.value = "Start";
Bolisrunning = false;
/*
* Statistical results
*/
println ("");
println ("Ping statistics for" + strURL + ":");
println ("packets:sent =" +
Intsent +
", Received =" +
INTRECV +
", Lost =" +
Intlost +
" (" +
Math.floor (intlost/intsent * 100) +
"% loss),");
if (intrecv = 0)
Return
println ("Approximate round trips times in Milli-seconds:");
println ("Minimum =" +
Math.min.apply (this, arrdelays) +
"MS, Maximum =" +
Math.max.apply (this, arrdelays) +
"MS, Average =" +
Math.floor (SUM/INTRECV) +
"MS");
}
Else
{
/*
* Start
*/
strURL = Objtxturl.value;
if (strurl.length = 0)
Return
if (strurl.substring (0,7). toLowerCase ()!= "http://")
strURL = "http://" + strurl;
Inttimeout = parseint ($ ("Txttimeout"). Value, 10);
if (isNaN (inttimeout))
inttimeout = 2000;
if (Inttimeout < 1000)
inttimeout = 1000;
Objbtn.value = "Stop";
Bolisrunning = true;
Arrdelays = [];
intsent = 0;
CLS ();
println ("Pinging" + strURL + ":");
println ("");
Ping ();
}
}
function println (str)
{
var objdiv = document.createelement ("div");
if (Objdiv.innertext!= null)
Objdiv.innertext = str;
Else
objdiv.textcontent = str;
Objcontent.appendchild (OBJDIV);
Objcontent.scrolltop = Objcontent.scrollheight;
}
Function CLS ()
{
objcontent.innerhtml = "";
}
</script>
</body>