Last active
August 29, 2015 13:58
-
-
Save cvan/9948621 to your computer and use it in GitHub Desktop.
using a dummy image to test whether we're online
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkOnline() { | |
// `navigator.onLine` is always accurate in Chrome, | |
// but of course it's *never* accurate in Firefox | |
// (bug 654579, bug 756364). Yeah, I know - sad times. | |
return new Promise(function (resolve, reject) { | |
var i = new Image(); | |
i.src = '/dummy.gif?' + +new Date(); | |
i.onload = function () { | |
resolve(); | |
}; | |
i.onerror = function () { | |
reject(); | |
}; | |
}); | |
} | |
checkOnline().then(function () { | |
console.log('online'); | |
}, function () { | |
console.log('offline'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4749 4638 3961 0100 0100 8000 0000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment