Skip to content

Instantly share code, notes, and snippets.

@taneltm
Created January 15, 2016 08:01
Show Gist options
  • Save taneltm/d33f41d69261ca04afa9 to your computer and use it in GitHub Desktop.
Save taneltm/d33f41d69261ca04afa9 to your computer and use it in GitHub Desktop.
<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