0

I have more than one script (for Google Charts) in the website and each one contains a loop (forEach) with $.Deferred() because I need it. When I have various scripts the website only show the result that the first has finished the ejecution and not the others. How I can wait until all scripts are finished to show the website.

I don't know if you can understand that I want to way.

BGG
  • 23
  • 1
  • 6
  • Use an array to keep track of the Deffereds, and then use apply and done to know when all of them have been resolved – adeneo Jan 17 '15 at 16:23
  • If you're having various `$.Deferred()` objects you can actually use [`$.when()`](http://api.jquery.com/jquery.when/) to check when all the objects have been resolved. – Terry Jan 17 '15 at 16:24
  • Bucle = Loop. Sorry. With a $.each – BGG Jan 17 '15 at 16:31
  • How is $.when()? The $.Deferred() are in differentes scripts and I need all of them before the website. Is for Google Charts and I need to add the data – BGG Jan 17 '15 at 16:32
  • 1
    You collect the Deferreds in an array, and then do `$.when.apply($, arr).done(function() { . ..` which is what I suggested in the first comment ! – adeneo Jan 17 '15 at 16:55
  • But de Deferreds are in different scripts. I want that all scripts are excuted fow show correctly Google Charts with the correct data. – BGG Jan 17 '15 at 17:04
  • If the `$.Deferred` objects are global, then of course you can use them between different files: http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files – Terry Jan 17 '15 at 17:13

1 Answers1

1

You write your scripts in footer or like:

 $(window).load(function() {
    //write your code.
 });
user254153
  • 1,855
  • 4
  • 41
  • 84
  • What? I have to put all the script inside your code? I have them now inside – BGG Jan 17 '15 at 16:33
  • Writing scripts inside this will execute only after all DOM has loaded in the website. – user254153 Jan 17 '15 at 16:39
  • But I want the scrips execute BEFORE the website to show Google Charts correctly – BGG Jan 17 '15 at 17:02
  • @BGG _"But I want the scrips execute BEFORE the website to show Google Charts correctly "_ Is requirement to execute several deferred processes _before_ calling google charts scripts ? – guest271314 Jan 17 '15 at 17:45
  • I use deferred to fill the data tables with a foreach from a JSON object. So, If I have a lot of charts I have a lot of deferred – BGG Jan 17 '15 at 17:51