Good morning companions.
I am trying to make a small script that monitors the status of jobs that are started on a server. I have the part of the response from the server and it works correctly, but it is when I check that activity every X seconds that I can't get the script to run.
For now I only get it to run the first time, when a particular button is pressed. When that button is pressed, all the code is executed, it reaches the statement of setInterval
, which calls a function traerEstado
with two parameters, and the function is executed but only once... when I think it should do it every X seconds indicated...
In this ajax to curl.php the only thing that is done is to pass the necessary parameters to a curl request that returns the idGenerated that we then need to pass to the fetchStatus function, to know if that job ID is still running, if it is waiting or if it has finished.
I attach my current code:
$('.btnProcess').click(function() {
var opcion = $(this).data('opcion');
var server = $(this).data('server');
var folder = $(this).data('folder');
var file = $(this).data('file');
var param = "opcion=" + opcion + "&folder=" + folder + "&file=" + file + "&server=" + server;
var resultado = $('.btnProcess[data-folder="' + folder + '"]').parent();
var box = $('.idBox[data-folder="' + folder + '"]');
if (opcion == 'tercios') var remove = 'recibos';
else var remove = 'tercios';
$.ajax({
type: 'POST',
url: 'exec/curl.php',
data: {param: param, server:server, file:file},
success: function(idGenerado) {
resultado.html("<img src='img/loading.gif' height='20' class='imgLoading' style='vertical-align:middle;'> <span class='txt'>Generando: "+folder+".pdf</span>");
box.val(idGenerado);
console.log("EXITO: " + idGenerado);
},
error: function(idGenerado) {
resultado.html("<img src='img/loading.gif' height='20' class='imgLoading' style='vertical-align:middle;'> <span class='txt'>Generando: "+folder+".pdf</span>");
box.val(idGenerado);
console.log("ERROR: " + idGenerado);
}
});
var repeticiones = setInterval(traerEstado(idGenerado, server), 1000);
});
function traerEstado(ID, srv) {
$.ajax({
type: "POST",
url: "exec/serverData.php",
data: {idGenerado:ID, server:srv},
success: function(respuesta) {
console.log(respuesta);
}
})
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btnProcess">Click Me</button>
The serverData.php file, right now, only does one echo date("H:i:s")
that is retrieved in the function and prints it on the console, from what I understand it should show me the current time on the console, every second, but as I said, it only prints it once and stands still.
One of the problems you have is in the following line:
As several things happen at the same time, I am going to separate them into two lines, the code is totally equivalent but surely as soon as you see it you will notice the error:
And the correct way to do it:
On the other hand, you should wait for the AJAX response to trigger this function or generatedid will be
undefined
, something like the following (a few more changes are needed, but you'll get the idea):In the setInterval it is not possible to execute every "x" time a function to which we pass some variables. This makes it only run once.
So you have two options to do it. Option 1: execute without calling another function.
Option 2: call another function without passing variables to it.