Function to send data to the bank after a while

-1

I need a function that after 1 minute and 30 seconds sends the data to the database. After much searching I could not do this dynamically I can do it with a div appearing after this time, but I wanted the user not to click on it to work.

Does anyone know how to do this feat?

    
asked by anonymous 23.12.2016 / 19:50

1 answer

1

To schedule an execution, you can use window.setTimeout , to send the data to the server, you can use XMLHttpRequest or $.ajax .

var dados = { prop1: "Hello", prop2: "World" };
var tempo = 90 * 1000;
window.setTimeout(function () {
  var httpRequest = new XMLHttpRequest();
  httpRequest.open("POST", urlParaSalvarOsDados, true);
  httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  httpRequest.addEventListener("readystatechange", function (event) {
    if (httpRequest.readyState == 4) {
      if (httpRequest.status == 200) {
        console.log("Dados enviados com sucesso");
      } else {
        console.log("Erro no envio dos dados");
      }
    }
  });
  httpRequest.send(JSON.stringify(dados))
}, tempo);
    
23.12.2016 / 19:59