当前位置:首页 > Web开发 > 正文

Lost = " + intLost + " (" + Math.floor(intLost / intSent *

2024-03-31 Web开发

jqueryping插件//要用的需要大白代码及本身改些处所

(function($) {
    $.fn.ping = function(options) {
        var opts = $.extend({}, $.fn.ping.defaults, options);
        var stime = new Date().getTime(); 
        return this.each(function() {
            var ping, requestTime, responseTime ;
            var target = $(this);
            function ping(){
                $.ajax({url: opts.getUrl(target.html())+‘/‘+ Math.random() + ‘.html‘,  //设置一个空的ajax请求
                    type: opts.type,
                    dataType: ‘html‘,
                    timeout: opts.timeout,
                    beforeSend : function() {
                        requestTime = new Date().getTime();
                    },
                    complete : function() {
                        responseTime = new Date().getTime();
                        ping = Math.abs(requestTime - responseTime);
                        $(‘#‘+target.html().replace(‘.‘,‘_‘)).text(ping + opts.unit);
                        //target.text(ping + opts.unit);
                    }
                });
            }
            var etime = new Date().getTime();
            if(Math.abs(stime - etime)<opts.timeout){
                ping();  //无论如何都要执行一次
                opts.interval != 0 && setInterval(ping,opts.interval * 1000);
            }
        });
    };
    $.fn.ping.defaults = {
        type: ‘GET‘,
        timeout: 10000,
        interval: 3,
        unit: ‘ms‘,
        isUrl:function(url){    //验证url是否有效
           var strReg = "^((https|http)?://)?" 
            + "(([0-9]{1,3}.){3}[0-9]{1,3}" // 
            + "|" // 
            + "([0-9a-z_!~*‘()-]+.)*" // 
            + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 
            + "[a-z]{2,6})" // 
            + "(:[0-9]{1,4})?" // 
            + "((/?)|" // 
            + "(/[0-9a-z_!~*‘().;?:@&=+$,%#-]+)+/?)$"; 
            var re=new RegExp(strReg); 
            return re.test(url);
           },
        getUrl:function(url){    //保证url
            var strReg="^((https|http)?://){1}"
            var re=new RegExp(strReg); 
            return re.test(url)?url:"http://"+url;
        }
    };
})(jQuery);

js ping url

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/33245.html