-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathDOMContentLoaded.js
57 lines (49 loc) · 1.63 KB
/
DOMContentLoaded.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2025 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var OS = require('../device/OS');
/**
* @callback ContentLoadedCallback
*/
/**
* 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.
*
* @function Phaser.DOM.DOMContentLoaded
* @since 3.0.0
*
* @param {ContentLoadedCallback} callback - The callback to be invoked when the device is ready and the DOM content is loaded.
*/
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);
}
else if (OS.cordova)
{
// 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;