Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Created October 19, 2017 02:48
Show Gist options
  • Save aloha1003/3b57189e876a681bbe36834b0d77a5c5 to your computer and use it in GitHub Desktop.
Save aloha1003/3b57189e876a681bbe36834b0d77a5c5 to your computer and use it in GitHub Desktop.
function ping(host, port, pong) {
var started = new Date().getTime();
var http = new XMLHttpRequest();
http.open("GET", "http://" + host + ":" + port, /*async*/true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
var ended = new Date().getTime();
var milliseconds = ended - started;
if (pong != null) {
pong(milliseconds);
}
}
};
try {
http.send(null);
} catch(exception) {
// this is expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment