How to keep page updated without refresh?

3

I'm trying to create a page that stays up-to-date without having to refresh.

I created a Javascript function that looks for select and while of another page, but this is generating high CPU consumption and the server faces have already complained. How do I always give select in Javascript without having this problem?

<!---- função que chama a pagina onde tem o while ----->
<script type="application/javascript">
function busca (){
        var numero1 = $("#n1").val();
        var numero2 = $("#n2").val();
        $.post("busca_p.php", {n1:numero1, n2:numero2}, function(retorno){
            $(".msg_porto").html(retorno);
            });

        };

</script>

<script>
   window.setInterval(busca,1000);// coloquei este para ativar a funlção a cada 1 segundo 
</script>

Calls the page busca_p.php that has a select and while and loads within div (msg_porto) .

It works fine, but gets processed all the time and has complained about high CPU usage by the server.

    
asked by anonymous 10.09.2015 / 05:17

2 answers

3

Your page refresh is fine, the problem is on the server side. Of course it can be improved, but the principle is what it uses.

The Ajax call is well done and serves the purpose. On the server side the only thing that can fail are the consequent requests , much in particular queries to the database. A recurring problem.

It is common for many environments to have only one server, where it serves as Http Server and Sql Server , but depending on the project and the database this can be a problem ( in>).

At this principle, every request HTTP can use many server resources, as its implementation seems to do. To minimize this, I advise you to use cache . Whether it is through proprietary software or a schema of its own you can save the result of your last SELECT to the database.

  

Something to avoid are generalist '*' SELECT statements in SQL , limiting the maximum number of records to return.

In programming it is not enough to just put some commands, we have to think about the implementation as a whole, but especially the purpose for which it is intended and the amount of data it will manage and move in every request .

To conclude, it is worth mentioning that on the client side you can do and redo following numerous recommendations ... but while on the server side things are not prepared for the reality of the project, nothing will be a solution.

    
10.09.2015 / 15:43
0

Probably not a very light query on the server side, the tables where the query is performed is fed every second? First it would be good to increase the query time, instead of 1 sec put 10 or 5 or even 1 min, this depends on the periodicity that the tables are updated. Web Sockets is a good one, in case you do not need to work in legacy browsers (especially in legacy IEs). Something that would help you a lot would be to create another table that stores the date and time of the last update of the tables that are part of this query, hence instead of every 1 sec to do the query, you first query this table and if there is any update so yes you query performs this query, this will greatly decrease the server's processing.

    
10.09.2015 / 20:02