Question

I have an unusual problem. I'm using the following script to check for internet connection using navigator.onLine. If there is internet connection, the page will be refreshed every 15 seconds. If there isn't any internet connection, the page will use innerHTML to display a message.

<script type="text/javascript">
setInterval(function () {
if (navigator.onLine) {
var myInterval = setInterval(function(){window.location.href = "Tracker.html";},15000);
} else {
clearInterval(myInterval);
var changeMe = document.getElementById("change");
change.innerHTML = "<big><font face='Arial' color='#ffffff' size='2'><center>OFFLINE</big><br>No internet connection</font></center>";
}
}, 250);
</script>

My problem is, once there is no internet connection, the message will be displayed, BUT the page would still be refreshed one last time. I'm trying to avoid this, by using clearInterval(myInterval); in the else part of the code, however it won't work.

Any suggestions?

Was it helpful?

Solution

To refresh the page at 15 second intervals (provided that a connection is present), use:

function refresh() {
    if(navigator.onLine)
        window.location.href = "Tracker.html";
    else{
        var changeMe = document.getElementById("change");
        change.innerHTML = "<big><font face='Arial' color='#ffffff' size='2'><center>OFFLINE</big><br>No internet connection</font></center>";
        setTimeout(refresh, 250);
    }
}
setTimeout(refresh, 15000);

At the end of 15 seconds, this checks whether a connection is present. If there is, it refreshes the page. If there isn't, it proceeds to check every 250 milliseconds afterwards until the user is reconnected, at which point it refreshes the page.

The net result is that the script refreshes the page as soon as possible after a minimum of 15 seconds have elapsed.

Here is a demonstration: http://jsfiddle.net/JGEt9/show

OTHER TIPS

Whenever the outer interval callback is executed, a new myInterval variable is created and the previous one is lost (it goes out of scope because the callback terminates).

You have to persist the value of the variable between function calls by declaring it outside of the function. You also have to make sure that you are not creating another timeout if one is already running.

var timeout = null;

setInterval(function () {
    if (navigator.onLine) {
        if (timeout === null) {
            timeout = setInterval(function(){window.location.href = "Tracker.html";},15000);
        }
    } else {
        clearTimeout(timeout);
        timeout = null;
        // ...
   }
}, 250);

You need to declare myInterval outside of the if statement. You should only need the refresh code once too. Something like this:

var myInterval = setTimeout(function(){window.location.href = "Tracker.html";},15000);
setInterval(function () {
  if (!navigator.onLine) {
    clearTimeout(myInterval);
    var changeMe = document.getElementById("change");
    changeMe.innerHTML = "<big><font face='Arial' color='#ffffff' size='2'><center>OFFLINE</big><br>No internet connection</font></center>";
  }
}, 250);

Here you set the refresh interval and continually check to see if the browser is offline, and if it is, you remove the timer and do your cleanup code. I also changed the refresh code to use setTimeout instead of interval because it only happens once.

Another issue is you create changeMe but then try to use change. change doesn't exist. I fixed that in my example as well.

Note: This will not resume refreshing once connection is regained. See Felix Kling's answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top