1 function ping(ip){ 2 var img = new Image(); 3 var start = new Date().getTime(); 4 img.src = "http://" + ip + "?t=" + start; 5 var flag = false; //无法访问 6 img.onload = function(){ 7 flag = true; 8 console.log('ok'); 9 }; 10 img.onerror = function(){ 11 flag = true; 12 console.log('ok'); 13 }; 14 var timer = setTimeout(function(){ 15 if(!flag){ //如果真的无法访问 16 flag = false; 17 console.log('failed'); 18 } 19 },1500); 20 } 21 ping('192.168.1.10')