Function to send data to the database after a while

Asked

Viewed 30 times

-1

I am in need of a function that after 1 minute and 30 seconds is sent the data to the database. After a lot of research I couldn’t do it dynamically I can do it with a div appearing after this time, but wanted it not be necessary for the user to click to work.

Someone knows how to do this feat ?

  • You’re probably looking for setInterval. But after 1 minute and 30 seconds with respect to what? Some click of the button, another event..

  • well the user will watch a video and after 1 minute and 30 seconds watching the video will be sent a function that will add 1 point to this user who will then be able to use these points for other things

1 answer

1

to schedule an execution, you can use the window.setTimeout, to send the data to the server, you can use the XMLHttpRequest or the $.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);

Browser other questions tagged

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