1

I need to run a function after a series of ajax has been resolved. The problem is that the series of ajax calls are in a $.each loop. I have tried using $.when around the $.each loop and then running the .done() function on it, but everything in the .done() is still running before the ajax is finished in the $.each loop. Here is my code -

$.when(
    $.each(images, function(key, value){
        var getFile = $(value).attr('src');
        $.ajax({
            type: 'HEAD',
            url: getFile,
            success: function() {
                var index = images.index(key);

                if (index > -1) {
                    images.splice(index, 1);
                }
            },
            error: function() {

            }
        });
        x++;
    });
).done(function(){
    // code to be executed after ajax done.
});

I had this working by using async: false, but I learned that is now deprecated and provides a terrible user experience for various reasons. Does anyone have any ideas? This is stumping me...

kiko carisse
  • 1,634
  • 19
  • 21
  • 2
    Whelp, my first question would be: why put an ajax request within an each loop? That's frowned upon. It creates a lot of requests and delays. My solution to this is generally to put the value's you're looping through into 1 big array, send this to my server and then return a json array filled with the results for each individual request, then loop over those requests in the script. It will speed up the process by probably 100 times. Is there any specific reason you're doing it seperately? – NoobishPro Feb 06 '17 at 23:11
  • The return value of the `.each()` function call is the array itself, not a Deferred object that `.ajax()` returns. This makes the `.when()` resolve immediately instead of waiting for the ajax call. You should append the return values of the `.ajax()` calls into an array and pass the array to `.when()`. See the duplicate question to see how. – Cave Johnson Feb 06 '17 at 23:18
  • But Babydead makes a good point. Why make all those requests instead of processing everything on the server at once. – Cave Johnson Feb 06 '17 at 23:21
  • I realize that looping ajax calls is a bad idea, but I couldn't think of another way. I am making a call to individual files for each image to check if they exist in a directory, is it possible to make a request to multiple files in one ajax call? – kiko carisse Feb 06 '17 at 23:23
  • 1
    @Kiko Carisse You can't request multiple headers, no. But the images are on your own server, right? So you can send the `src` attributes to your server in an array, and check whether or not they exist or not from there. (with PHP this would be `file_exists();`. Your server most likely caches your files and also doesn't have to make requests, since it's all local. Then you can return the results (true or false) for each of said sources. Or are the images not on your own server? In that case there is indeed no other way. – NoobishPro Feb 06 '17 at 23:29
  • @Babydead the images are on my server yes. I wasn't able to use `file_exists()` as it returned false no matter what, but this did lead me down the road to success. People were saying that I had to do something with safe mode on the server to get `file_exists()` to work, but wasn't really interested in messing with the server as I am making a plugin. I ended up using this to check whether or not the file exists [link](http://stackoverflow.com/questions/6930150/file-exists-returns-false-but-the-file-does-exist/42080299#42080299) Could you tell me if I'll have problems with this in the future? – kiko carisse Feb 07 '17 at 02:51
  • @kikocarisse if it works, it works. I don't see a problem with that. I don't know about any "safe mode" either. I personally think you are simply messing up the path. `file_exists()` checks from the document root, not the site root. You could use `$root = realpath($_SERVER['DOCUMENT_ROOT']` and then check like so: `if(file_exists("$root/uploads/imagefolder/img.jpg"));`. That should work, I believe. Anyway, glad you could figure it out. Doing it server side should still be lots better than client-side :) – NoobishPro Feb 07 '17 at 09:52

0 Answers0