I can not effectively do that while executing the readSuccessB(evt,file)
function, pause the loop and resume right after finishing that function.
I have tried callbacks creating variables so that if it is running do not call the function readSuccessA
, but by then, the value i
already adds 1
and can not execute that function. I tried subtracting 1
to i
, but I created a infinite loop .
My code:
function openFiles(evt){
var files =$(evt)[0].files;
function anadir_archivo(evt){
var total_subidas=files.length;
for (var i = 0; i < total_subidas; i++) {
var file=files[i];
reader = new FileReader();
if(total_subidas==1||i+1==total_subidas){
reader.onload = readSuccessA(evt,file);
}else{
reader.onload = readSuccessB(evt,file);
}
function readSuccessA(evt,file) {
return function(e){
$('fieldset.subir_imagen>legend+div+label+div+input[type=file]').appendTo($(evt).prev().prev().prev('div').append('<div title="'+file.name+'"><img src="'+e.target.result+'" alt="No se pudo cargar" /><div>'+file.name+'</div></div>').children()).attr({'id':'','onchange':''});
}
}
function readSuccessB(evt,file) {
return function(e){
$(evt).prev().prev().prev('div').append('<div title="'+file.name+'"><img src="'+e.target.result+'" alt="No se pudo cargar" /><div>'+file.name+'</div></div>');
}
}
reader.readAsDataURL(evt.files[i]);
}
}