Skip to content

Instantly share code, notes, and snippets.

@djom202
Created June 9, 2016 22:30
Show Gist options
  • Select an option

  • Save djom202/d8710c65a052d1a17bbcbc434af6a8d5 to your computer and use it in GitHub Desktop.

Select an option

Save djom202/d8710c65a052d1a17bbcbc434af6a8d5 to your computer and use it in GitHub Desktop.
Verify Status Internet
window.connStatus = {
_status: null,
waitForChange: function(_callback) {
this.getState();
this.updateState(_callback);
return this;
},
updateState: function(_callback) {
console.log('Init setInterval');
setInterval(function() {
if (this._status != navigator.onLine) {
this._status = navigator.onLine ? true : false;
console.log('Cambio!', navigator.onLine);
_callback(this._status);
} else {
console.log('Pendiente al cambio...');
}
}, 200);
return this;
},
getState: function() {
this._status = navigator.onLine ? true : false;
return this;
}
};
<!doctype html>
<html>
<head>
<script src="connStatus.js"></script>
</head>
<body>
<p id="state"></p>
<script>
connStatus.waitForChange(function(_status) {
var condition = _status ? "ONLINE" : "OFFLINE",
state = document.getElementById("state");
state.innerHTML = condition;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment