Created
November 23, 2011 04:27
-
-
Save avioli/1387894 to your computer and use it in GitHub Desktop.
Simple Pinger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ping.txt is exactly 100 bytes | |
pinger = (callback, file_to_ping) -> | |
ajax = start = done = null; | |
ajax = if window.XMLHttpRequest then (new XMLHttpRequest()) else (new ActiveXObject('MSXML2.XMLHTTP.3.0')) | |
if !ajax | |
return callback?.call null, 0 | |
start = new Date() | |
ajax.onreadystatechange = -> | |
if ajax.readyState > 0 | |
done = new Date() | |
ajax.onreadystatechange = null | |
callback?.call null, done.valueOf() - start.valueOf() | |
ajax.open 'HEAD', if file_to_ping then file_to_ping else 'ping.txt' | |
ajax.send() | |
latency = 0 | |
calls = 0 | |
pinger (ms) -> | |
latency += ms | |
calls++ | |
pinger (ms) -> | |
latency += ms | |
calls++ | |
pinger (ms) -> | |
latency += ms | |
calls++ | |
pinger (ms) -> | |
latency += ms | |
calls++ | |
latency /= size * calls | |
go(latency) # do your stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment