1

I'm using jquery load() to load multiple items. All works good.

for(i = 0; i<item.length; i++){
   $( '#parent'+i).load('file'+i+'.html',function(){
       customFunctions();
   });
}

My question is, I need to run customFunctions() when all completes loading. My above code runs it multiple times. How can I run customFunctions() once only after all files complete loading?

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 1
    Why dont you just call it at the end of `$(document).ready()`? @Becky – Varun Jul 03 '15 at 13:45
  • Your posted code doesn't really make sense. Aren't you passing any data to server? Otherwise, why calling the exact same request in a for loop? – A. Wolff Jul 03 '15 at 13:47
  • @Varun thanks but this is triggered after the page loads. So `$(function() { }`will not work. – Becky Jul 03 '15 at 13:47
  • Of course it is called multiple times – you are calling it inside a loop!? – feeela Jul 03 '15 at 13:47
  • @Varun You don't really need any of those ready events – just include all of your JS at the end of the HTML file. – feeela Jul 03 '15 at 13:48
  • @feeela thanks - I know. That's my question I've inlucded it inside the for loop because I'm not certain if it should be out of the `load()`'s callback. – Becky Jul 03 '15 at 13:49
  • 1
    You should use [$.ajax](http://api.jquery.com/jquery.ajax/) instead. That will return a promise-like object which you can use [$.when](https://api.jquery.com/jquery.when/) on to check that all of the requests are finished – CodingIntrigue Jul 03 '15 at 13:49
  • 1
    Wait, you'd have better to explain what is your expected behaviour instead – A. Wolff Jul 03 '15 at 13:50

5 Answers5

4

It is about making promises, but you could also do this:

var all_loaded = 0;

for(var i = 0; i < item.length; i++){
   $( '#parent' + i).load('file' + i + '.html',function(){
      ++all_loaded == item.length && customFunctions();
   });
}

//++all_loaded: mean It is sum +1 when a file is loaded

So when all_loaded is equal to item.length, the function is loaded just at the end.

1

Add counter and update it when each file is loaded. When current value of counter will be equal to total files count you want to load, then call your function;

Viktor Kireev
  • 1,200
  • 1
  • 8
  • 17
0
for(var i = 0; i<item.length; i++){
   $( '#parent').load('file.html',function(){
      if(i==(item.length-1))
          customFunctions();
   });
}

This should work.

Varun
  • 1,946
  • 2
  • 11
  • 17
0

Try This:

Your function will run only if all load request is successful

var item = {length:12};
runfunction = 0;
for(i = 0; i<item.length; i++){
   $( '#parent').load('t.html',function(responseText, textStatus, XMLHttpRequest){

      if (textStatus == "success") {
            runfunction++;
       }
       /* you can write three line of code any where even after for loop according to your requirements*/
       if(runfunction == item.length ){
        customFunctions();
       }
   });
}

function customFunctions(){
 alert(1);
}
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
0

There are a number of options.

You can just keep track in your loop:

var loadedCount = 0;
for(i = 0; i<item.length; i++){
  $( '#parent').load('file.html',function(){
    loadedCount++;
    if (loadedCount === item.length) {
      customFunctions();
    }
  });
}

That will get the job done for this specific case. This only works in this constrained design where you know how many times you are calling and can trust/believe that none of your AJAX requests will fail. As a result, it is not a very stable design.

Sam R
  • 696
  • 3
  • 7