Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continue in for loop after function is done JavaScript

I have found behaviour that makes me trouble in JavaScript. I have two functions:

var show_uploaded = function( data_photo ) that gets informations about uploaded photos as a parameter,

function exif_like(path){/*working with my image*/ console.log('exif_like END'); }
that gets path to every single one uploaded photo as a parameter.

I made code like this:

var show_uploaded = function( data_photo ){
    for( var z = 0; z < data_photo.length; z++ ){ 
       var send_image_name = uploaded_photo[z].name;
       $.ajax({
            data: { compare : send_image_name },
            url: 'get_panorama.php',
            method: 'POST', // or GET
            success: function( path ){
                exif_like( path );
                console.log('FOR LOOP END');
            }
       });
    }
};

But what I see in console is that "FOR LOOP END" log is displayed before "exif_like END" log. And this causes some unpredictable behaviour in my code.

Is there any solution how to continue for loop after exif_like() function is done?

like image 728
Petr Bečka Avatar asked Sep 03 '25 13:09

Petr Bečka


1 Answers

If the remainder of processes return expected results, and exif_like actually only performs console.log() you could try using $.when()

$.when(exif_like( path )).then(function() {console.log("FOR LOOP END")})
like image 80
guest271314 Avatar answered Sep 05 '25 05:09

guest271314