Created
January 15, 2016 08:01
-
-
Save taneltm/d33f41d69261ca04afa9 to your computer and use it in GitHub Desktop.
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
<script type="text/javascript" src="cordova.js"></script> | |
<script type="text/javascript"> | |
(function() { | |
var isDeviceReady = false; | |
var onDeviceReady = function() { | |
console.log('whenDeviceReady:onDeviceReady') | |
isDeviceReady = true | |
}; | |
var onDocReady = function() { | |
console.log('whenDeviceReady:onDocReady') | |
document.addEventListener('deviceready', onDeviceReady, false); | |
}; | |
document.addEventListener('DOMContentLoaded', onDocReady, false); | |
window.whenDeviceReady = function(callback) { | |
if (typeof cordova == "undefined") { | |
// In browser | |
console.log('whenDeviceReady:browser') | |
isDeviceReady = true; | |
callback(); | |
} else if (isDeviceReady) { | |
// Cordova is already ready | |
console.log('whenDeviceReady:ready') | |
callback(); | |
} else { | |
// Cordov isn't ready yet, queue the callback | |
console.log('whenDeviceReady:queued') | |
document.addEventListener('deviceready', callback, false); | |
} | |
}; | |
})(); | |
</script> | |
<script> | |
// Can add multiple callbacks | |
whenDeviceReady(function() { ... }); | |
whenDeviceReady(function() { ... }); | |
whenDeviceReady(function() { ... }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment