Jquery POST with pause in sending

Asked

Viewed 43 times

0

Good morning. I need to do several POST using Jquery, but they need to be sent one by one, paused.

Example: All survey data would be sent, then all environment data and so on.

Problem

As the server has character limitation in the POST, I can not send everything at once, it has to be sent in a paused form, and so you do not risk losing any record.

Problem of my created solution

This data is sent via cell phone, and for some reason sometimes does not send all the data.

My JS code

I tried to create a function sleep:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

//ENVIO N1
tx.executeSql('SELECT * FROM vistorias WHERE codigo=?', [codigo], async function (tx, rsVistoria) {
      let rowsVistoria = rsVistoria.rows;
      for (var i = 0; i < rowsVistoria.length; i++) {
        $.post("https://url.com.br/arquivo.php", {
          acesso:'vistorias',
          codigo: rowsVistoria[i].codigo,
          fotoFachada: rowsVistoria[i].fotoFachada
        });
        await sleep(1000);
      }
    })


//ENVIO N2
tx.executeSql('SELECT * FROM vistorias_OUTRA WHERE codigo=?', [codigo], async function (tx, rsVistoria) {
      let rowsVistoria = rsVistoria.rows;
      for (var i = 0; i < rowsVistoria.length; i++) {
        $.post("https://url.com.br/arquivo.php", {
          acesso:'vistorias',
          codigo: rowsVistoria[i].codigo,
          fotoFachada: rowsVistoria[i].fotoFachada
        });
        await sleep(1000);
      }
    })

My intention with the code above

Make each FOR wait 1 second, to send the other record. After the end of the ENVIO N1, passes to the ENVIO N2, and so on.

Someone would have a better suggestion?

  • No Jquery manjo, do not know what the return of the post, if it is a Promise you can give await in it, if the return is another, you can encapsulate in a Promise, define how async and then give a await

  • Take a look at this documentation: https://api.jquery.com/jQuery.getJSON/

  • Wouldn’t it be better to give up Sleep and use the method $.ajax with the option of async: false?

  • @Benilson how I do it?

  • @Benilson could post an example?

  • Please take a look at the accepted answer on this link https://answall.com/questions/6392/awaitr-retorno-de-ajax-em-fun%C3%A7%C3%A3o-s%C3%Adncrona

  • @Benilson I put each Insert in one function foo () { different?

  • I’ll put it in answer, so you understand better

  • @Benilson Thanks.

  • where $.post... you exchange for $.ajax... and stop using Sleep.

Show 5 more comments

1 answer

1


Perhaps this is one of the few cases where synchronous ajax requests should be used:

$.ajax(
    {
        url: "https://url.com.br/arquivo.php",
        type: 'POST',
        async: false, // define que pára tudo até a requisição finalizar
        // dataType: 'html', // define um tipo de retorno, talvez json
        data: {
            acesso:'vistorias',
            codigo: rowsVistoria[i].codigo,
            fotoFachada: rowsVistoria[i].fotoFachada
        },
        success: function (response) {
           // faz alguma coisa ao finalizar alguma requisição
        }
    }
);
  • I came back. Just to understand. It will be 5 AJAX that I will create, one for each INSERT. O async: false, makes it only pass to the next ajax, when the current one ends?

  • Yes, any javascript action will only be executed after executing the ajax request.

  • Take this example, is that right? https://jsfiddle.net/xetc2ak9/

  • I confess I find this method very strange tx.executeSql, and don’t know how it works, you make the query in the frontend accessible for the user to see? The data sent in the request ENVIO N1 are exactly the same as ENVIO N2 as in the question code?

  • The tx.executeSql is to handle Sqlite/Websql data. About SENDING N1, and SENDING N2, I have begged with the same information only to inform you that there will be more than one. This JS code is inside the phone. And I need to send the data that is saved in Websql to the Mysql online server.

  • So, the fiddle code is exactly the same as the code I posted, which I did replacing the question you posted, the operation will be exactly the same as $.post, only it will run in an orderly fashion, if a request takes half a second or two, it won’t matter, it will only execute the next request after finishing the previous one. The only thing I don’t know how it’s gonna work is async function of tx.executeSql.

  • Which would be better, keep using my code or use yours?

  • You have to test, comment on the Sleep and exchange the $.post(... for the $.ajax(.... If it doesn’t work, post a new comment or edit the question stating that it didn’t work, maybe someone else will give you another answer that will help you better.

  • Searching in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/funcoes_assincronas, I found that this one async function has to be modified to function.

  • Benilson, there’s something very interesting going on! That’s in my and your code. Supposing ENVIO N1 has registration in Sqlite, but the ENVIO N2 has no record, the code for ENVIO N1 and does not go to the next. I added a value in the ENVIO N2 Then he went from N1 to N2, strange this! Can you tell me if you need to do something else?

  • See if you can help me: https://answall.com/questions/433520/help_para-crackingerro-codigo-para-se-n%C3%a3o-tiver-registro-na-tabela-websql

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.