HTML5 提供的方案
if (navigator.onLine) {
console.log('ONLINE!');
} else {
console.log('Connection flaky');
}
window.addEventListener('online', function(e) {
// Re-sync data with server.
}, false);
window.addEventListener('offline', function(e) {
// Queue up events for server.
}, false);