html 发送ping帧,用JS实现ping的功能(JS ping url)

这是一个jquery的ping插件,实现了js来ping url的功能。当然这个和系统上的ping不是一回事,一个是ping使用icmp协议,一个是使用http协议,获取响应时间罢了。好了,上代码

$.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);

}

}; 来一个实例:

$.ping({

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

beforePing : function(){$('#msg').html('')},

afterPing : function(ping){$('#msg').html(ping)},

interval : 1

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http-ping是一个小型的,免费的,易于使用的Windows命令行实用程序,它可以探测给定的URL并显示相关的统计信息。 它与流行的ping工具类似,但是通过HTTP / S而不是ICMP工作,并且使用URL而不是计算机名/ IP地址。 http-ping支持IPv6地址。 http-ping提供了一组丰富的命令行选项,可以通过运行“http-ping.exe /?”来查看。从DOS提示符下: http-ping [-t] [-n count] [-i interval] [-f file-name] [-s] [-v] [-q] [-c] [-r] [-w timeout] [-p] [-d] [-o data | -of file-name] [-ua user-agent] [-h header-line] [-e | -et | -eb] [-ipv4 | -ipv6] URL Where: URL The URL you wish to check. For example, http://www.kiva.org, http://209.191.122.70, or http://www.yourhost:8080. Be sure to surround an IPv6 address with square brackets. For example, http://[2a00:1450:4007:800::1014]. Options: -t Ping the specified URL until stopped. To see statistics and continue - type Control-Break; To stop - type Control-C. -n count Send 'count' requests. Supercedes -t. -i interval Wait 'interval' seconds between each request. There is a 1-second wait if this option is not specified. -f file-name Save responses to file 'file-name'. Please specify the full path, and use quotes around file names with spaces. -s Silent. Print no output. -v Verbose. Print detailed output. Supercedes -s. -q Quick. Perform HTTP HEAD requests instead of GETs. This will retrieve headers only, and bytes reported will be 0. -c Perform a full connection on each request; ignore keep-alive. -r Follow HTTP redirects. -w timeout Wait 'timeout' seconds for a response before timing out. Specify 0 to avoid timing out. If not specified, the default timeout is 30 seconds. -p Use the proxy defined in the Windows Internet settings. -d Print the date and time of each ping attempt. -o dat

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值