Calling a function to refresh the page only

1

I have the following HTML that calls a JavaScript function:

 <div>
  <button id="avancar-aula">Avançar aula</button> 
 </div>

 <script src="js/avanca-aula.js"></script>

And I have the JS function:

let avancarAula = document.querySelector('#avancar-aula');
avancarAula.addEventListener('click', function(evento){

 avancaAula();
});

function avancaAula(){


      let avanca = {

        idUsuario: document.querySelector('#id').textContent,
        token: document.querySelector('#token').textContent,
        id: document.querySelector('#ultima-aula').textContent
      };

      var xhr = new XMLHttpRequest();
      var variavel = document.querySelector('#token-servico').innerHTML;

      xhr.open("POST", "http://54.233.9.248:8080/web/rest/classes/nexts", 
true);
      xhr.setRequestHeader("Content-type", "application/json");
      xhr.setRequestHeader("Authorization", "Bearer " + variavel);
      xhr.addEventListener("load", function(){

      if(xhr.status == 200){

       console.log(xhr.responseText);
     }

     if(xhr.status =500){
       console.log(xhr.responseText);
    }
   });
  xhr.send(JSON.stringify(avanca));
 }
setTimeout(avancaAula, 3000);

My problem is as follows.

Every time I give an f5 it calls the function, like it clicks the button alone. I made it so that by the time I want to change the page I click the button to call the action, but I can not imagine what is happening for this function to be called the moment the page is updated. What can it be?

    
asked by anonymous 18.05.2018 / 02:08

1 answer

0
  

[...] but I can not imagine what's happening for this function to be called in   the page is refreshed. What can it be?

The setTimeout(avancaAula, 3000); will call the function after 3 seconds when the page loads.

If you do not want this to happen, just delete this line from SetTimeout .

    
18.05.2018 / 02:32