How to Implement the ping function using pure Javascript
This article mainly introduces how to implement the ping function using pure Javascript. The example analyzes the tips of javascript to implement the ping function, which has some reference value. If you need it, you can refer
This example describes how to implement the ping function using pure Javascript. Share it with you for your reference. The specific implementation method is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Function ping (ip ){ Var img = new Image (); Var start = new Date (). getTime (); Var flag = false; Var isCloseWifi = true; Var hasFinish = false; Img. onload = function (){ If (! HasFinish ){ Flag = true; HasFinish = true; Img. src = 'x :\\'; Console. log ('ping' + ip + 'success .'); } }; Img. onerror = function (){ If (! HasFinish ){ If (! IsCloseWifi ){ Flag = true; Img. src = 'x :\\'; Console. log ('ping' + ip + 'success .'); } Else { Console. log ('network is not working! '); } HasFinish = true; } }; SetTimeout (function (){ IsCloseWifi = false; Console. log ('network is working, start ping ...'); }, 2 ); Img. src = 'HTTP: // '+ ip +'/'+ start; Var timer = setTimeout (function (){ If (! Flag ){ HasFinish = true; Img. src = 'x ://'; Flag = false; Console. log ('ping' + ip + 'fail .'); } },1500 ); } Ping ('www .google.com: 80 '); |
I hope this article will help you design javascript programs.