Skip to content

Instantly share code, notes, and snippets.

@thomasdunn
Last active April 26, 2019 04:44
Show Gist options
  • Save thomasdunn/e10b7d49aa9f06482030 to your computer and use it in GitHub Desktop.
Save thomasdunn/e10b7d49aa9f06482030 to your computer and use it in GitHub Desktop.
xhr pinger
<!DOCTYPE html>
<html>
<head>
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="initial-scale=1">
<style type="text/css">
body {
background: black;
}
#status {
color: white;
font-family: sans-serif;
font-size: 3em;
margin: 20px;
padding: 20px;
border: 10px solid red;
border-radius: 10px;
}
</style>
<body>
<div id="status">
Waiting for response...
</div>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
document.getElementById("status").innerText = "Response received " + new Date();
timeout();
});
xhr.addEventListener("error", function() {
document.getElementById("status").innerText = "Error occurred " + new Date();
timeout();
});
tick();
function tick() {
xhr.open("get", window.location.href + "?" + Date.now());
xhr.send();
}
function timeout() {
setTimeout(function() {
tick();
}, 1000);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment