Skip to content

Instantly share code, notes, and snippets.

@BorisChumichev
Last active June 29, 2016 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BorisChumichev/f185792dafd78283ab37ccd3636734bb to your computer and use it in GitHub Desktop.
Save BorisChumichev/f185792dafd78283ab37ccd3636734bb to your computer and use it in GitHub Desktop.
`deviceorientation` deep check
(function () {
var URL_SUCCESS = ''
, URL_FAILURE = ''
function checkSupport(supported, notSupported) {
var failureTimeout = null
function attachEvent() {
if (window.addEventListener)
window.addEventListener('deviceorientation', checkEventData, true)
else
window.attachEvent('ondeviceorientation', checkEventData)
}
function detachEvent() {
if (window.removeEventListener)
window.removeEventListener('deviceorientation', checkEventData, true)
else
window.detachEvent('ondeviceorientation', checkEventData)
}
function checkEventData(event) {
if (event.alpha !== null) {
supported()
clearTimeout(failureTimeout)
detachEvent()
}
}
function checkHardwareSupport() {
failureTimeout = setTimeout(function () {
detachEvent()
notSupported()
}, 1500)
attachEvent()
}
'DeviceOrientationEvent' in window
? checkHardwareSupport()
: notSupported()
}
checkSupport(
function () {
document.createElement('img').src = URL_SUCCESS
},
function () {
document.createElement('img').src = URL_FAILURE
}
)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment