phaser/src/dom/DOMContentLoaded.js
2018-01-26 03:40:49 +00:00

62 lines
1.7 KiB
JavaScript

var OS = require('../device/OS');
var isBooted = false;
/**
* Inspects the readyState of the document. If the document is already complete then it invokes the given callback.
* If not complete it sets up several event listeners such as `deviceready`, and once those fire, it invokes the callback.
* Called automatically by the Phaser.Game instance. Should not usually be access directly.
*
* @function Phaser.Dom.DOMContentLoaded
* @since 3.0.0
*
* @param {function} callback - The callback to be invoked when the device is ready and the DOM content is loaded.
*
* @return {boolean} Returns `false` if the document is already loaded, otherwise `true` if the callback is pending.
*/
var DOMContentLoaded = function (callback)
{
if (isBooted)
{
return false;
}
if (document.readyState === 'complete' || document.readyState === 'interactive')
{
isBooted = true;
callback();
return true;
}
var check = function ()
{
isBooted = true;
document.removeEventListener('deviceready', check, true);
document.removeEventListener('DOMContentLoaded', check, true);
window.removeEventListener('load', check, true);
callback();
};
if (!document.body)
{
window.setTimeout(check, 20);
}
else if (OS.cordova && !OS.cocoonJS)
{
// Ref. http://docs.phonegap.com/en/3.5.0/cordova_events_events.md.html#deviceready
document.addEventListener('deviceready', check, false);
}
else
{
document.addEventListener('DOMContentLoaded', check, true);
window.addEventListener('load', check, true);
}
return true;
};
module.exports = DOMContentLoaded;