加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

测时延

发布时间:2020-12-16 00:20:07 所属栏目:百科 来源:网络整理
导读:!DOCTYPE html html head meta charset="UTF-8" titleInsert title here222222222222/title script type="text/javascript" src="js/jquery-1.8.0.js"/script script type="text/javascript" $.ping = function(option) { var ping,requestTime,responseTime

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here222222222222</title>

<script type="text/javascript" src="js/jquery-1.8.0.js"></script>

<script type="text/javascript">

$.ping = function(option) {

var ping,requestTime,responseTime;

var getUrl = function(url) {

// 保证url带http://

var strReg = "^((https|http)?://){1}"

var re = new RegExp(strReg);

return re.test(url) ? url : "http://" + url;

}

$.ajax({

url : getUrl(option.url) + '/' + (new Date()).getTime() + '.html',

// 设置一个空的ajax请求

type : 'GET',

dataType : 'html',

timeout : 10000,

beforeSend : function() {

if (option.beforePing)

option.beforePing();

requestTime = new Date().getTime();

},

complete : function() {

responseTime = new Date().getTime();

ping = Math.abs(requestTime - responseTime);

if (option.afterPing)

option.afterPing(ping);

}

});


if (option.interval && option.interval > 0) {

var interval = option.interval * 1000;

setTimeout(function() {

$.ping(option)

},interval);

// option.interval = 0; // 阻止多重循环

// setInterval(function(){$.ping(option)},interval);

}

};

</script>

<script type="text/javascript">

function t() {

$.ping({

url : 'http://www.9188.com/',

beforePing : function() {

$('#msg0').html('')

},

afterPing : function(ping) {

$('#msg1').html(ping)

},

interval : 5

});


}

</script>

</head>

<body>

<a href="javascript:;" onclick="t();">reqServer</a>

<br>

<div id="msg0"></div>

<div id="msg1"></div>

</body>

</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读