Dynamically loading Javascript files and load completion events

前端 未结 3 1005
攒了一身酷
攒了一身酷 2021-01-19 07:32

today I\'ve been working on loading dynamic javascript code (files). The solution I use is :

function loadScript(scriptUrl) {
        var head = document.get         


        
3条回答
  •  误落风尘
    2021-01-19 07:41

    You could use a counter variable, and a kind of callback:

    var scriptsToLoad = 10;
    var loadedScripts = 0;
    //...
    function increaseLoadCount() {
    loadedScripts++;
    if(loadedScripts == scriptsToLoad) {
    alert("start here");
    }
    }
    
    //script1-10.js
    increaseLoadCount();
    

    Maybe a bit nicer than with a timeout..

提交回复
热议问题