{"comment":{"url":"https://api.github.com/repos/Slynchy/FNAF-JS/comments/12482819","html_url":"https://github.com/Slynchy/FNAF-JS/commit/fc094193ad85d586092890f43e84438804fe7e56#commitcomment-12482819","id":12482819,"user":{"login":"chry5ls","id":8003556,"avatar_url":"https://avatars.githubusercontent.com/u/8003556?v=3","gravatar_id":"","url":"https://api.github.com/users/chry5ls","html_url":"https://github.com/chry5ls","followers_url":"https://api.github.com/users/chry5ls/followers","following_url":"https://api.github.com/users/chry5ls/following{/other_user}","gists_url":"https://api.github.com/users/chry5ls/gists{/gist_id}","starred_url":"https://api.github.com/users/chry5ls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chry5ls/subscriptions","organizations_url":"https://api.github.com/users/chry5ls/orgs","repos_url":"https://api.github.com/users/chry5ls/repos","events_url":"https://api.github.com/users/chry5ls/events{/privacy}","received_events_url":"https://api.github.com/users/chry5ls/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":"","commit_id":"fc094193ad85d586092890f43e84438804fe7e56","created_at":"2015-08-02T11:47:06Z","updated_at":"2015-08-02T11:47:06Z","body":"Here's the loading script I'm using\r\n\r\n```\r\nvar Loader = {}; //namespace\r\nLoader.timer = {};  // contains timers for scripts\r\nLoader.scripts = [];  // contains called script references\r\nLoader.version = '0.0.0';\r\nLoader.loaded = 0;\r\nLoader.finish = function(){};\r\nLoader.load = function(url, callback) {\r\n    // handle object or path\r\n    var classname = null;\r\n    var properties = null;\r\n    try {\r\n        // make sure we only load once\r\n        if (Loader.scripts.indexOf(url) == -1) {\r\n            // note that we loaded already\r\n            Loader.scripts.push(url);\r\n            var script = document.createElement(\"script\");\r\n            script.src = url+'?v='+Loader.version;\r\n            script.type = \"text/javascript\";\r\n            script.charset = 'utf-8';\r\n            document.querySelector('head').appendChild(script);  // add script tag to head element\r\n\r\n            // was a callback requested\r\n            if (callback) {\r\n                // test for onreadystatechange to trigger callback\r\n                script.onreadystatechange = function () {\r\n                    if (script.readyState == 'loaded' || script.readyState == 'complete') {\r\n                        callback();\r\n                    }\r\n                }\r\n                // test for onload to trigger callback\r\n                script.onload = function () {\r\n                    Loader.loaded += 1;\r\n                    callback();\r\n                    if (Loader.loaded == Loader.scripts.length) {\r\n                        console.info('[Loader] All ('+Loader.loaded+') scripts are loaded.', Date.now());\r\n                        Loader.finish();\r\n                    }\r\n                    return;\r\n                }\r\n                // safari doesn't support either onload or readystate, create a timer\r\n                // only way to do this in safari\r\n                /*if ((Prototype.Browser.WebKit && navigator.userAgent.match(/Version\\/[12]/)) || Prototype.Browser.Opera) { // sniff\r\n                    Loader.timer[url] = setInterval(function() {\r\n                        if (/loaded|complete/.test(document.readyState)) {\r\n                            clearInterval(Loader.timer[url]);\r\n                            callback(); // call the callback handler\r\n                        }\r\n                    }, 10);\r\n                }*/\r\n            } else {\r\n                script.onload = function () {\r\n                    Loader.loaded += 1;\r\n                    if (Loader.loaded == Loader.scripts.length) {\r\n                        console.info('[Loader] All ('+Loader.loaded+') scripts are loaded.', Date.now());\r\n                        Loader.finish();\r\n                    }\r\n                }\r\n            }\r\n        } else {\r\n            if (callback) { callback(); }\r\n        }\r\n    } catch (e) {\r\n        alert('Loader:   ' + e);\r\n    }\r\n}\r\n```\r\n\r\nHow to use:\r\n```\r\n// load core components here.\r\nLoader.load('app/core/cookies.js');\r\nLoader.load('app/core/events.js', function() { // when this file already loaded, then do this code\r\n    Events.register('miracle_loaded'); // you can already call code from script above\r\n    Loader.load('app/core/language.js', function() {\r\n        Loader.load('app/app.js');\r\n    });\r\n});\r\n\r\n// finally, this will be called after all scripts are loaded\r\nLoader.finish = function() {\r\n    core.init();\r\n    core.load();\r\n    Events.emit('miracle_loaded');\r\n};\r\n```"}}