首页 > 其他 > 详细

测时延

时间:2014-10-14 03:02:49      阅读:259      评论:0      收藏:0      [点我收藏+]

<!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>


测时延

原文:http://ether007.blog.51cto.com/8912105/1563572

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!