I am adding a plugins system to a program and I want the user to be able to include files and raw information. I have a problem, however with loading the scripts. I want to wait for all the scripts to load (I don't know the number of them, however). An example is this:
Plugins.json:
{"include":["script1.json", "script2.json"]}
main file ("pluginsFile" variable is the content of "Plugins.json"):
/*I have a system that lets the user import the plugins file*/
loadedScripts = 0;
if (pluginsFile.include){
for (i of pluginsFile.include){
var scriptToAdd = document.createElement("script");
scriptToAdd.src = pluginsFile.include[i];
scriptToAdd.addEventListener("load", function(){
loadedScripts++;
})
document.head.appendchild(scriptToAdd);
}
while (loadedScripts < document.querySelectorAll("head > script")){//Assume that I only have the scripts I want to add inside the head}
//After all the scripts loaded
console.log("loaded");
}
However, when I import the file, the while loop doesn't stop and the tab lags. I want to wait for all the scripts I insert into <head> to be loaded and then continue. I this possible?
while (loadedScripts <= document.querySelectorAll("head > script").length) { ... }querySelectorAll()Promiseor switching tosetInterval).