Receive Jquery and PHP response from second to second

0

Galera,

I have a page in PHP that does a query in the database at the end of the processing and I show the amount of updated records. This process takes a while, in my Jquery I just show the total of affected records, how could I change my Jquery to show from time to time, in case every 1 second it shows the user the amount of records. >

EDIT: ATTENTION I can not keep calling the PHP page several times, I need to call it once and listen to it or receive data from it!

var form_data = new FormData();

         form_data.append('id', idSel);
         $.ajax({
             url: 'postar_reenvio.php', // caminho para o script que vai processar os dados
             type: 'POST',
             data: form_data,
             cache: false,
             contentType: false,
             processData: false,
             beforeSend: function () {
                $('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> <br>Enviando Mensagens, aguarde!");
            },
             success: function(response) {
    
asked by anonymous 31.05.2017 / 16:21

3 answers

1

I believe that for the purpose you want, a Websocket is a lot better than ajax requests every 1 second. With websocket you would see it in real time.

You would make your own script that is running the querys send the information through the socket channel.

In php you can use these examples:

link

link

    
31.05.2017 / 18:18
0

I believe in the function itself

  

beforeSend ();

You can create a loop by stating the number of records entered. (If I understand it that way.)

    
31.05.2017 / 16:25
0

You can use window.setInterval of JS to call the function by passing the function you want to call as a parameter and the time in milliseconds.

The code would look like this:

var intervalo = window.setInterval(consulta, 1000);
function consulta() {
   var form_data = new FormData();
     form_data.append('id', idSel);
     $.ajax({
         url: 'postar_reenvio.php', // caminho para o script que vai processar os dados
         type: 'POST',
         data: form_data,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> <br>Enviando Mensagens, aguarde!");
        },
         success: function(response) {
        }
   }

And if you want to stop the script use clearInterval(intervalo); .

    
31.05.2017 / 16:37