What is an efficient way of waiting for a dynamically loaded Javascript file to finish loading?
<p>I'm loading a Javascript file that has a function in it that I'd like to call. This is not possible, since between 'initiating' the load, and actually calling the function, the JS isn't loaded yet. Of course, I could do something like <code>setTimeout('functionCall();',5000);</code>, but I don't think this is a very efficient method, and it seems really unstable to me. That's why I was wondering whether there was a better way to do it.</p>

<p>Here's the code I'm using. The function in question is called <code>controllerStart</code>. If I comment out the last line here, and type it into a Javascript terminal (like on Chrome developer tools), everything works.</p>

<pre><code>    function loadController(name){
        clearAll();
        scriptContainer.innerHTML = '';
        var scriptElement = document.createElement('script');
        scriptElement.setAttribute('src','controllers/' + name + '.js');
        scriptElement.setAttribute('type','text/javascript');
        scriptContainer.appendChild(scriptElement);
        controllerStart();// &lt;-- Doesn't work from here, but works on a terminal
    }
</code></pre>

<p>Thanks for taking a look!</p>
