Checking the users internet connection

Hi everyone,

I’ve been doing a lot of work sending and receiving data from a database within my hype documents however I’m having some issues with problems that occur if the users internet connection drops.

For example, my JavaScript functions still initiate if there’s no internet connection and throws up warning in the developer console however my hype document acts as if the action was completed as it should. This is mainly an issue with the use of Ajax to send and receive data from the database.

So I was wondering if anyone had a solution for this? I was thinking of maybe adding some lines of code at the start of every connection reliant JavaScript function to check the connection or a new JavaScript function which checks internet connection at the start of a scene (then maybe direct to another scene if there’s no connection detected).

What avenue does everyone think I should take to solve this?

Thanks,

Chris

Perhaps using the “Navigator” interface to check if browser is online/offline.

e.g

if(navigator.onLine){
  // do something 
} else {
 // do something if not
}

You could probably add this logic to the call for action when posting your AJAX call.

or

navigator does have an event listener you can also use

window.addEventListener('offline', function(e) { console.log('offline'); });

window.addEventListener('online', function(e) { console.log('online'); });

You could then do some logic when the connection drops or comes back online. Maybe run this on scene load.

2 Likes

Excellent! Thanks for this @DBear. I’ll give both idea a good to see which works best for the project :+1:

Update

First suggestion works great :+1: thanks again @DBear

Note that navigator.onLine has definite caveats:

Is it possible for you to use error callbacks (if using jquery) or looking at the readyState/status codes returned from the XMLHTTPRequest instead?