码迷,mamicode.com
首页 > 其他好文 > 详细

测时延

时间:2014-10-14 03:38:18      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:function   return   option   ajax   title   

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


测时延

标签:function   return   option   ajax   title   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!