phaser/src/dom/DOMContentLoaded.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var OS = require('../device/OS');
2018-03-19 21:43:48 +00:00
/**
* @callback ContentLoadedCallback
*/
2017-10-12 14:09:52 +00:00
/**
2018-01-26 03:40:49 +00:00
* 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 accessed directly.
2017-10-12 14:09:52 +00:00
*
2018-02-13 00:40:51 +00:00
* @function Phaser.DOM.DOMContentLoaded
2017-10-12 14:09:52 +00:00
* @since 3.0.0
*
2018-03-19 21:43:48 +00:00
* @param {ContentLoadedCallback} callback - The callback to be invoked when the device is ready and the DOM content is loaded.
2017-10-12 14:09:52 +00:00
*/
var DOMContentLoaded = function (callback)
{
if (document.readyState === 'complete' || document.readyState === 'interactive')
{
callback();
return;
}
var check = function ()
{
document.removeEventListener('deviceready', check, true);
document.removeEventListener('DOMContentLoaded', check, true);
window.removeEventListener('load', check, true);
callback();
};
if (!document.body)
{
window.setTimeout(check, 20);
}
2016-11-25 04:33:48 +00:00
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);
}
};
module.exports = DOMContentLoaded;