当前位置 : 主页 > 网络编程 > JavaScript >

javascript模拟ping判断ip是否连通

来源:互联网 收集:自由互联 发布时间:2021-06-30
ping function ping(ip,callback){ if(!/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(ip)){ if(callbackcallback.error){callback.error(); } return; } var img = new Image(); var start = new Date().getTime(); var flag
ping
function ping(ip,callback){	
  if(!/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(ip)){
    if(callback&&callback.error){
		callback.error();
	  }
  	return;
  }
  var img = new Image();
  var start = new Date().getTime();
  var flag = false;
  var isCloseWifi = false;
  var hasFinish = false;
  img.onload = function() {
    if ( !hasFinish ) {
      flag = true;
      hasFinish = true;
	  if(callback&&callback.success){
		callback.success();
	  }
    }
  };
  img.onerror = function() {
    if ( !hasFinish ) {
      if ( !isCloseWifi ) {
        flag = true;
		if(callback&&callback.success){
			callback.success();
		}
      } else {
        if(callback&&callback.error){
			callback.error();
		}
      }
      hasFinish = true;
    }
  };
  img.src = 'http://' + ip + '/' + start;
  setTimeout(function() {
    if ( !flag ) {
      hasFinish = true;
      flag = false ;
      if(callback&&callback.error){
		callback.error();
	  }
    }
  }, 50);
};
网友评论