Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delay running some JS code until ALL of my asynchronous JS files downloaded?

UPDATE:

I have the following code:

<script type="text/javascript">
function addScript(url) {
    var script = document.createElement('script');
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
}   
addScript('http://google.com/google-maps.js');
addScript('http://jquery.com/jquery.js');

...

// run code below this point once both google-maps.js & jquery.js has been downloaded and excuted

</script>

How can I prevent code from executing until all required JS have been downloaded and executed? In my example above, those required files being google-maps.js and jquery.js.

like image 708
Henryh Avatar asked May 10 '10 04:05

Henryh


People also ask

How do you make a delay in JavaScript?

The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.

How do I delay a script loading?

If you cannot do that for some reason, then you can delay the loading of the external script file like this: setTimeout(function() { var headID = document. getElementsByTagName("head")[0]; var newScript = document. createElement('script'); newScript.

Is there a wait function in JavaScript?

JavaScript does not provide any native functions like wait() for timing events. When it comes to Timing Events in JavaScript, there are the following functions that you can use in your project. Many programming languages have the sleep function that will wait for the program's execution for a given number of seconds.


1 Answers

You can use the onload event of the script element for most browsers, and use a callback argument:

Edit: You can't really stop the execution of the code when you load scripts in this way (and making synchronous Ajax requests is a bad idea most of the times).

But you can chain callbacks, so if you have some code that depends on both, Two.js and Three.js, you can chain the loading actions, for example:

loadScript('http://example.com/Two.js', function () {
  // Two.js is already loaded here, get Three.js...
  loadScript('http://example.com/Three.js', function () {
    // Both, Two.js and Three.js loaded...
    // you can place dependent code here...
  });
});

Implementation:

function loadScript(url, callback) {
  var head = document.getElementsByTagName("head")[0],
      script = document.createElement("script"),
      done = false;

  script.src = url;

  // Attach event handlers for all browsers
  script.onload = script.onreadystatechange = function(){
    if ( !done && (!this.readyState || // IE stuff...
      this.readyState == "loaded" || this.readyState == "complete") ) {
      done = true;
      callback(); // execute callback function

      // Prevent memory leaks in IE
      script.onload = script.onreadystatechange = null;
      head.removeChild( script );
    }
  };
  head.appendChild(script);
}

For IE, the onreadystatechange event has to be bound.

like image 137
Christian C. Salvadó Avatar answered Nov 14 '22 02:11

Christian C. Salvadó