SOURCE

console 命令行工具 X clear

                    
>
console
/* ping.js - v0.2.1 http://github.com/alfg/ping.js */
var Ping=function(a){this.opt=a||{},this.favicon=this.opt.favicon||"/favicon.ico",this.timeout=this.opt.timeout||0};Ping.prototype.ping=function(a,b){function c(a){d&&clearTimeout(d);var c=new Date-e;if("function"==typeof b)return"error"===a.type?(console.error("error loading resource"),b("error",c)):b(null,c)}this.img=new Image;var d,e=new Date;this.img.onload=c,this.img.onerror=c,this.timeout&&(d=setTimeout(c,this.timeout)),this.img.src=a+this.favicon+"?"+ +new Date},"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(module.exports=Ping):window.Ping=Ping;


/*var p = new Ping();

p.ping("http://192.168.1.135", function(err, data) {
  // Also display error if err is returned.
  if (err) {
    console.log("error loading resource")
    data = data + " " + err;
  }
  document.getElementById("ping-github").innerHTML = data;
});*/

setInterval(()=>{
    vping('127.0.0.1', function(res){
        console.log(res);
    });
}, 2000);

function vping(ip, callback) {
    if (!this.inUse) {
        this.status = 'unchecked';
        this.inUse = true;
        this.callback = callback;
        this.ip = ip;
        var _that = this;
        this.img = new Image();
        this.img.onload = function () {
            _that.inUse = false;
            _that.callback('responded');
        };
        this.img.onerror = function (e) {
            if (_that.inUse) {
                _that.inUse = false;
                _that.callback('responded', e);
            }
        };
        this.start = new Date().getTime();
        this.img.src = "http://" + ip;
        this.timer = setTimeout(function () {
            if (_that.inUse) {
                _that.inUse = false;
                _that.callback('timeout');
            }
        }, 1500);
    }
}
<div id="ping-github"></div>