Home > Web Front-end > JS Tutorial > body text

JavaScript simulated Ping effect code (Web Ping)_javascript skills

WBOY
Release: 2016-05-16 18:09:32
Original
1980 people have browsed it

Of course, the length of the HTTP header cannot be counted during the request, so when the request packet length is at the critical point of the maximum transmission unit, additional HTTP headers can cause IP packets, so there is a certain error. (2009/6/21)

Online demo: http://demo.jb51.net/js/2011/ping/

Core code:

Copy code The code is as follows:



<머리>

HTTP Ping
<스타일>
html
{
높이: 100%;
오버플로: 숨김;
}
본문
{
배경: #000;
색상: #C0C0C0;
글꼴 두께: 굵게;
글꼴 크기: 14px;
글꼴군: Lucida Console;
높이: 100%;
여백: 0 0 0 5px;
}
#divContent
{
높이: 90%;
오버플로: 자동;
}
#txtTimeout
{
너비: 40px;
}
버튼
{
여백-왼쪽: 10px;
}


<본문>

URL:

시간 초과:


<시간/>


<스크립트>
var intStartTime;
var objIMG = 새 이미지();
objIMG.onload =
objIMG.onerror =
function()
{
/*
* 有回应,取消超时计时
*/
clearTimeout(intTimerID );
if(!bolIsRunning || bolIsTimeout)
return;
var Delay = new Date() - intStartTime;
println(""
strURL
" 시간의 응답"
((delay<1)?("<1"):("="delay))
"ms" );
arrDelays.push(지연);
/*
* 每次请求间隔限system 1秒以上
*/
setTimeout(ping, Delay<1000?(1000-delay):1000);
}
function ping()
{
/*
* 发送请求
*/
intStartTime = new Date();
intSent ;
objIMG.src = strURL "/" intStartTime;
bolIsTimeout = false;
/*
* 超时计时
*/
intTimerID = setTimeout(timeout, intTimeout);
}
함수 timeout()
{
if(!bolIsRunning)
return;
bolIsTimeout = true;
objIMG.src = "X:\";
println("요청 시간이 초과되었습니다.");
핑();
}

<스크립트>
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 handlerBtnClick()
{
if(bolIsRunning)
{
/*
* 停止
*/
var intRecv = arrDelays.length;
var intLost = intSent-intRecv;
var sum = 0;
for(var i=0; isum = arrDelays[i];
objBtn.value = "시작";
bolIsRunning = false;
/*
* 统计结果
*/
println(" ");
println("" strURL "에 대한 핑 통계:");
println("  Packets: Sent = "
intSent
", Receivered = "
intRecv
", Lost = "
intLost
" ("
Math. Floor(intLost / intSent * 100)
"% 손실),");
if(intRecv == 0)
return;
println("밀리초 단위의 대략적인 왕복 시간:");
println("  Minimum = "
Math.min.apply(this, arrDelays)
"ms, Maximum = "
Math.max.apply(this, arrDelays)
"ms, 평균 = "
Math.floor(sum/intRecv)
"ms");
}
else
{
/*
* 开始
*/
strURL = objTxtURL.value;
if(strURL.length == 0)
return;
if(strURL.substring(0,7).toLowerCase() != "http://")
strURL = "http://" strURL;
intTimeout = parsInt($("txtTimeout").value, 10);
if(isNaN(intTimeout))
intTimeout = 2000;
if(intTimeout < 1000)
intTimeout = 1000;
objBtn.value = "중지";
bolIsRunning = true;
arrDelays = [];
intSent = 0;
cls();
println("핑 " strURL ":");
println(" ");
핑();
}
}
function println(str)
{
var objDIV = document.createElement("div");
if(objDIV.innerText != null)
objDIV.innerText = str;
그 외
objDIV.textContent = str;
objContent.appendChild(objDIV);
objContent.scrollTop = objContent.scrollHeight;
}
함수 cls()
{
objContent.innerHTML = "";
}



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template