Calling a page update function

Asked

Viewed 35 times

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 this.

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

  • 1

    The setTimeout(avancaAula, 3000); is calling the function when the page is loaded.

1 answer

0


[...]but I can’t imagine what’s going on for this function to be called on time when the page is updated. What can be?

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

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

  • Yes, I tested it here and that’s really it, but I need the setTimeout, how can I replace it?

  • But you need him on what occasion?

  • I need to understand this better so I can know what to do without having to erase it.

  • It is that the function needs the iID of the last class in this line, ID: Document.querySelector('#last-class').textContent.... however, the ID comes from an API. When the page is loading, if I don’t place this team, it can’t get the id in time. Kind of loads the whole page and the ID comes later. setTimeout delays loading this function, causing it to appear after the ID has been loaded.

  • Right. This is not the best way to do it. I’m going to look here to see what you should do.

  • Thank you very much.

  • @Francisvagnerdaluz "(...) can’t catch the id in time." - You’re talking about this:: id: document.querySelector('#ultima-aula') ? If this is the case it is because javascript is running before the page loads, in which the correct way to resolve is to run in the event load of window

  • @Francisvagnerdaluz To not leave this question open and as I was the only one to answer, it would be interesting to close it by marking in the answer. If you have any more questions, you can call me at any time and I will be happy to help. Obg!

Show 3 more comments

Browser other questions tagged

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