Last active
June 29, 2016 15:47
-
-
Save BorisChumichev/f185792dafd78283ab37ccd3636734bb to your computer and use it in GitHub Desktop.
`deviceorientation` deep check
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 () { | |
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