0

I have this JS loop:

          for (var i = 0; i < jsArr.length; ++i) {
            $.post("checkdata.php", { names: jsArr[i], platforms: "1" })
            .done(function(data) {
                eval(data);
            });
          }

data is some jQuery CSS manipulation. I have a loading image that is under the "loadingImg" div class which is display on page load. checkdata.php checks the names against the API https://www.xboxleaders.com/

What's the best way to accomplish $('#loadingImg').hide(); (to hide the image) after the loop and the jQuery .done processes complete?

I have thought about adding it after the loop, but that does not guarantee checkdata has finished.

4
  • 2
    Do you really need all those Ajax calls? can't you send all data at once? Commented Jul 20, 2013 at 22:23
  • Perform your .hide as part of your .done callback. You'll need to track whether you're on the last iteration. Of course, if you can send ll the data at once (thanks @koala_dev) then there are fewer calls, faster response, and no tracking to consider Commented Jul 20, 2013 at 22:27
  • You can just send for example namesArr : jsArr and send data for all images not only for one. Commented Jul 20, 2013 at 22:27
  • The first thing you should do, is remove that eval() Commented Jul 20, 2013 at 22:35

4 Answers 4

3

$.when can be used to combine deferreds, and will resolve, when all of them are resolved.

var defs = [];
for (var i = 0; i < jsArr.length; ++i) {
  defs.unshift($.post("checkdata.php", { names: jsArr[i], platforms: "1" }));
  defs[0].done(function(data) {
    eval(data);
  });
}

$.when.apply($, defs).done(function() {
  $('#loadingImg').hide();
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1 looks about right, hard to test, but putting the deferreds in an array and calling $.when is the way to go.
I also haven't tested it, so take it with a grain of salt
1

Use a counter:

      var counter = 0;
      for (var i = 0; i < jsArr.length; ++i) {
        $.post("checkdata.php", { names: jsArr[i], platforms: "1" })
        .done(function(data) {
            counter++;
            eval(data);
            if(counter == jsArr.length){
                $('#loadingImg').hide();
            }
        });
      }

1 Comment

or use i as smerny suggested, even better
0

You better use waterfall

This question would be helpful.

Comments

0

One solution to this is by creating a second array that has the same number of elements as jsArr. On each .done, set the corresponding index of the second array to true. Then in each call of .done, check if all the values of the second array are true, and if so, hide. (Or you could just push a new value into the second array and check if the length of the arrays are the same for the hide to take effect)

doneChecks = [];
for (var i = 0; i < jsArr.length; ++i) {
    $.post("checkdata.php", { names: jsArr[i], platforms: "1" })
        .done(function(data) {
            doneChecks.push(true);
            eval(data);
            if (doneChecks.length == jsArr.length)
                $('#loadingImg').hide(); 
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.