There used to exist offline.js for detection of a connection, but that project is no longer maintained. Also, the browser's 'navigator.onLine' property is notoriously unreliable.
What is the best/most up-to-date method to use to actively detect the browser's online state, compatible with modern browsers including mobile device browsers, that can raise various events when the online status changes? Do I need to write this myself, or is that reinventing the wheel?
Note that nobody in my userbase is going to be changing the "Work Offline" setting in the browser in order to go online/offline. Instead, they will be unplugging network cables, disconnecting from wifi networks, driving to locations where no signals are available, etc.
My thought was to simply set up a timeout to repeatedly make AJAX calls to a URL on my server, and simply set an 'online' variable to true/false as well as call online/offline callback functions based on error/success of the AJAX call. Is that the recommended way?