Skip to content

Instantly share code, notes, and snippets.

@oraccha
Created November 9, 2010 14:16
Show Gist options
  • Save oraccha/669122 to your computer and use it in GitHub Desktop.
Save oraccha/669122 to your computer and use it in GitHub Desktop.
/*
* jQuery ping
* Original code: http://ha.ckers.org/weird/xhr-ping-sweep.html
*/
<script type="text/javascript" src="./jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="./timer.js"></script>
<script type="text/javascript">
url = "http://twitter.com/";
function log(data) {
var text;
text = document.createElement('div');
text.innerHTML = data + "<br>";
document.body.appendChild(text);
}
function ping(url) {
var d = new Date();
$.ajax({
type: "GET",
url: url,
cache: false,
timeout: 60000,
success: function(html, status, req) {
var d2 = new Date();
var time = d2.getTime() - d.getTime();
if (req.status == 200 && time < 18000) {
if (time > 10) {
$("#rtt").html(time + "ms.");
}
} else {
log("error! status: " + req.status);
}
},
error: function(req, status, error) {
log("error! status: " + req.status + " error: " + error);
}
});
}
$(function() {
$("#url").html(url);
$.timer(5000,function(timer) {
ping(url);
});
})
</script>
<table border="0">
<tr><td id="url"></td><td>:</td><td id="rtt"></td></tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment