Skip to content

Instantly share code, notes, and snippets.

@avioli
Created November 23, 2011 04:27
Show Gist options
  • Save avioli/1387894 to your computer and use it in GitHub Desktop.
Save avioli/1387894 to your computer and use it in GitHub Desktop.
Simple Pinger
# 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