I have an ajax function that executes every 3 seconds, calling a 'file2.php' file. I pass a variable $lastid through POST, use it in 'file2.php' and then in the response i get a new last id, which I need to send as $lastid the next time the ajax function executes. But I can't find a way to update the variable that actually replaces the previous value in the ajax call.

I have tried assigning response.lastid to var lastid inside the ajax function but it doesn't help. I keep getting the same response I got in the previous call, eternally.

This is the ajax function and how I set the variable initially

```
<script>
var lastid = <?php echo $lastid; ?>;

function nuevospedidos() {
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "file2.php", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var response = JSON.parse(this.responseText);
      console.log(response);
      var lastid = response.lastid;
    }
  };
  xhttp.send("lastid=" + lastid);

}
setInterval(nuevospedidos, 3000);
</script>

```

The response I get is this JSON object

```
{lastid: "4074",
contenido: "some content",
originalid: "4073"}

```

I would like to receive that response only once, and then update the lastid variable with the lastid that the JSON object brings, so that the next time it executes it knows that was the last id processed.