Skip to content

Instantly share code, notes, and snippets.

@kamlekar
Last active March 17, 2017 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamlekar/fb5c433b0a3f8786b52a2eaaf0f12644 to your computer and use it in GitHub Desktop.
Save kamlekar/fb5c433b0a3f8786b52a2eaaf0f12644 to your computer and use it in GitHub Desktop.
Used to load files synchronously. (useful to show progress of loading files)
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