I'm working on a function that checks internet connectivity (works fine) and a function that refreshes the page every 20 seconds. Another function I've been working on detects when the internet state changes and stops the refresh function. I'm trying to make it work so when the internet comes back online, the refresh function starts again. I've tried multiple fixes but nothing is working.
Here's some code:
function checkNetConnection()
{
var xhr = new XMLHttpRequest();
var file = "http://i.imgur.com/FIKb6D8.png";
var r = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + r, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
function modalCheck()
{
var status = checkNetConnection();
if(status == false) {
document.getElementById('mymodal').style.display = "block";
} else {
document.getElementById('mymodal').style.display = "none";
}
}
var int1 = setInterval(modalCheck, 7000);
function refreshPage()
{
var state = checkNetConnection();
if(state == false)
{
clearInterval(int2);
} else {
location.reload(true);
alert("PAGE RELOADED!"); /* Testing purposes! */
}
}
var int2 = setInterval(refresh, 12000);
Everything works fine until the internet connection comes back online, then the refresh function doesn't start again. This is what I'm trying to fix.
Thanks.