Last active
March 17, 2017 08:05
-
-
Save kamlekar/fb5c433b0a3f8786b52a2eaaf0f12644 to your computer and use it in GitHub Desktop.
Used to load files synchronously. (useful to show progress of loading files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadFiles(files, fn) { | |
var head = document.head || document.getElementsByTagName('head')[0]; | |
var counter = files.length; | |
callback(); // To cover case when there are no files available | |
files.forEach(loadFile); | |
function loadFile(file) { | |
var fileref = document.createElement('script'); | |
fileref.setAttribute('type', 'text/javascript'); | |
fileref.setAttribute('src', file); | |
head.appendChild(fileref); | |
// Used to call a callback function | |
fileref.onload = function () { | |
counter--; | |
callback(); | |
}; | |
} | |
function callback(){ | |
if(counter <= 0){ | |
if(fn){ | |
fn(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment