How to call function in JS only once

3

Well, I have the following problem: I am building a chat page, I am already receiving good messages, but when I enter this chat page I wanted it to automatically go down to the bottom of the page, where the most recent messages would be.

Follow the code:

function ajax(){
    var req = new XMLHttpRequest();
    req.onreadystatechange = function(){
        if (req.readyState == 4 && req.status == 200) {
            document.getElementById('mensagens').innerHTML = req.responseText;
        }
    }

    req.open('GET', 'mensagens.php', true);
    req.send();
    final();
}

setInterval(function(){ajax();}, 1000);

var x = 0;

function final(){
    if(x == 0){
        parent.scroll(0, 10000);
        x++;
    }
}

If I remove this if (from the final function) leaving only the parent.scroll (0, 10000); it will come down to the end of one in a second, however I want to go only the 1st time the page is accessed. Would there be another way to stop this function without being the one I'm using? (Detail, this form used by me is not working and, also, I could not stop it with a return or break.)

    
asked by anonymous 18.06.2018 / 16:33

4 answers

1

Use window.scrollTo() after setTimeout :

document.addEventListener("DOMContentLoaded", function(){
   setTimeout(function(){
      window.scrollTo(0, 10000);
   }, 10);
   setInterval(ajax, 1000);
});

function ajax(){
   // aqui o código do ajax
}
Início
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
Fim!
    
18.06.2018 / 16:55
0

Dude, you set a setInterval every 1 second, it will always go to the end of the chat. Only thing you have to do to run it once when opening the page is to take the setInterval and directly execute the function with ajax()

    
18.06.2018 / 16:45
0

You can use the onload function in the HTML page:

<body onload="final()">

It will execute soh when the user enters the page, then you have to remove the call in the Ajax code to not be giving load all the time:

function ajax(){
    var req = new XMLHttpRequest();
    req.onreadystatechange = function(){
        if (req.readyState == 4 && req.status == 200) {
            document.getElementById('mensagens').innerHTML = req.responseText;
        }
    }

    req.open('GET', 'mensagens.php', true);
    req.send();
}

Support link: link

    
18.06.2018 / 16:47
0

Just remove final(); from within Ajax so it does not go to the end of the chat every 1 second, and call final(); after popular.

window.onload = function(){ //Quando TODO o documento for carregado. 
    function ajax(){
        var req = new XMLHttpRequest();
        req.onreadystatechange = function(){
            if (req.readyState == 4 && req.status == 200) {
                document.getElementById('mensagens').innerHTML = req.responseText;
            }
        }
        req.open('GET', 'mensagens.php', true);
        req.send();
    }
    function final(){
        ajax(); //Se certificar de que o chat está atualizado
        parent.scroll(0, 10000);
    }
    final(); //Depois de popular o chat, ir para o final.  
    setInterval(function(){ajax();}, 1000); //Atualiza a cada 1 segundo

}
    
18.06.2018 / 16:54