Skip to content

Instantly share code, notes, and snippets.

@HazzazBinFaiz
Created November 4, 2021 11:04
Show Gist options
  • Save HazzazBinFaiz/8a09986b586a2fefaf77641366d948df to your computer and use it in GitHub Desktop.
Save HazzazBinFaiz/8a09986b586a2fefaf77641366d948df to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Online Status Detector</title>
<style type="text/css">
#indicator {
min-height: 100vh;
width: 100%;
display: flex;
justify-content: center;
font-size: 5rem;
align-items: center;
}
</style>
</head>
<body>
<div id="indicator"></div>
<script type="text/javascript">
let flusher = null;
let body = document.getElementsByTagName('body')[0];
let indicator = document.getElementById('indicator');
function startFlush() {
if (flusher == null) {
flusher = setInterval(function(){
if (body.style.backgroundColor == 'green') {
body.style.backgroundColor = 'white'
document.title = 'Online'
} else {
body.style.backgroundColor = 'green'
document.title = '~~~~'
}
}, 100);
}
}
function stopFlush() {
if (flusher != null) {
clearInterval(flusher);
flusher = null
}
body.style.backgroundColor = 'white'
document.title = 'Offline'
}
function onlineChecker() {
setInterval(function(){
fetch('https://jsonplaceholder.typicode.com/todos/1').then(function(){
startFlush();
indicator.innerHTML = 'Online'
}).catch(function(ex){
stopFlush();
indicator.innerHTML = 'Offline'
})
}, 1000);
}
onlineChecker();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment