Last active
April 26, 2019 04:44
-
-
Save thomasdunn/e10b7d49aa9f06482030 to your computer and use it in GitHub Desktop.
xhr pinger
This file contains hidden or 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
<!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