I have an ajax function which is recursive, which means that it returns me an answer and if it returns a code I'll call it back to itself sending the code it gave me back, and so on until I return it in the code the value of -1, there ends the recursion.
function actualizarProm (curs_para_mate_prof_codi,peri_dist_codi,es_hija,mensaje,firstTime)
{ var xmlhttp;
/*Agrego la data*/
var tabla_info = document.getElementById("tabla_info");
if (firstTime==1)
while(tabla_info.rows.length > 1)
tabla_info.deleteRow(-1);
var fila = tabla_info.insertRow(-1);
var cellMensaje = fila.insertCell(0);
var cellProgreso = fila.insertCell(1);
cellMensaje.innerHTML = mensaje;
cellProgreso.innerHTML = '<div id="prog_info_'+peri_dist_codi+'"><img src="../../imagenes/ajax-loader.gif"/></div>';
var data = new FormData();
data.append("peri_dist_codi", peri_dist_codi);
var peri_dist_padr_previous = peri_dist_codi;
data.append("curs_para_mate_prof_codi", curs_para_mate_prof_codi);
data.append("es_hija", es_hija);
data.append("opc", "actualizar_prom");
if (window.XMLHttpRequest)
{ xmlhttp = new XMLHttpRequest ();
}
else
{ xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function ()
{ document.getElementById('prog_info_'+peri_dist_codi).innerHTML="Finalizado";
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ obj = JSON.parse(xmlhttp.responseText);
if (obj.error == "no" && obj.peri_dist_codi!=-1 && obj.peri_dist_codi!=null)
{ actualizarProm(curs_para_mate_prof_codi,obj.peri_dist_codi,es_hija,obj.mensaje,0);
}
else
{ if (obj.error == "no")
{ if (obj.mensaje==null)
actualizarProm( curs_para_mate_prof_codi, peri_dist_padr_previous, es_hija, "Reintentando", 0 );
else
{ var fila = tabla_info.insertRow(-1);
var cellMensaje = fila.insertCell(0);
var cellProgreso = fila.insertCell(1);
cellMensaje.innerHTML = obj.mensaje;
cellProgreso.innerHTML = "Actualización de notas completa";
}
}
else
{ var fila = tabla_info.insertRow(-1);
var cellMensaje = fila.insertCell(0);
var cellProgreso = fila.insertCell(1);
cellMensaje.innerHTML = obj.mensaje;
cellProgreso.innerHTML = "No se ha completado la actualización de notas, intente nuevamente y luego comunique a sistemas.";
}
}
}
}
xmlhttp.open("POST", "script_actualizar_prom.php", false);
xmlhttp.send(data);
}
The problem here is that even when the php does not return anything, the recursion is still done. That is, how could I make it wait until the server responds and there continue with the next recursion, and so it does not remain cycled. Because what is on the line
if (xmlhttp.readyState==4 && xmlhttp.status==200)
It does not work, since it enters so that the php has not returned anything yet. In the php file I call a stored procedure which runs and takes a while. I hope you have understood me. Thanks for your valuable help.