this.loadRequiredFiles = function (callback) { var scripts = ['version.js', 'global.js', 'JsHttpRequest.js', 'keydown.keycodes.js', 'keydown.observer.js', 'watchdog.js', 'usbdisk.js', 'load_bar.js', 'xpcom.common.js', 'xpcom.webkit.js', 'blocking.js', 'player.js']; var filesloaded = 0; var filestoload = scripts.length; var i = 0; onLoadScript(); function finishLoad() { //console.log('Loading finish'); if (filesloaded === filestoload) { //console.log('Load callback'); callback(); } } function onLoadScript() { if ( i < scripts.length){ //console.log('Loading script ' + scripts[i]); var script = document.createElement('script'); script.type = 'text/javascript'; script.src = scripts[i]; script.onload = function () { //console.log('Loaded script ' + scripts[i]); filesloaded++; // (This means increment, i.e. add one) i++; onLoadScript(); }; document.head.appendChild(script); } else { finishLoad(); } } };