How to keep page updated without refresh?

Asked

Viewed 1,399 times

3

I’m trying to create a page that is always updated, without having to give refresh.

I created a Javascript function that looks for a select and while from another page, but this is generating high consumption in the CPU and the server guys already complained. How do I give forever 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>

Call the page busca_p.php who has a select and while and carries within the div (msg_porto).

Works well, but is processing all the time and have complained of high CPU use by the server.

  • People maybe messed up a little what I tried to pass, but what I try to do is to have my page updated always without having to update the page, so I made a function that calls a page where this consulting the bank and giving a while . So bring the result and click on div (msg_port). And even works but consumes a lot of server cpu because I put the function window.setInterval(search,1000); to call the function that requests the page query every 1 second.

  • Javascript does the function of prompting the server for the information that will be returned by PHP without having to refresh the full page. Check this link http://www.mauricioprogramador.com.br/posts/actualizr-parte-do-site-sem-refresh-na-pagina-inteira-com-javascript I hope I helped you.

  • Dario helped me yes , knowledge is always welcome, but I need my page to update without the need of the user action, and I achieved this only using the function window.setInterval(search,1000) but this makes the server process a lot , I thought if I could have this without having to access a page off , but I might be talking nonsense, but thanks for trying to help me

  • What code are you running on the server? Can you put it here? and what HTML is returning? Without that information we can’t help much more.

  • <div class="msg_port">...</div> //A div that returns with the result

  • Using the angular framework, it may solve your problem, since it is dynamic. I don’t know how to use it, but who knows it can even guide you better.

  • Use Web Socket for this, apparently what you want is to have a real-time in your application and Web Socket helps to solve precisely this problem and with a much lower CPU consumption, follows a good introduction: http://www.html5rocks.com/pt/tutorials/websockets/basics/

  • @Andrénascimento, I believe that Angular alone does not solve his problem, after all he will still need to consult the server to update the model. on the other hand, there may be something in PHP that does what Meteorjs does, a Model bind connecting the client to the server.

  • @Cesarlimapaulo, The new content is created by your application? I mean, another user accesses another page of the system to insert the new data? if yes, you can use Web Sockets to notify all customers who need this new information.

Show 4 more comments

2 answers

3

Your page update is not bad, the problem is on the server side. Of course it can be improved, but the principle is what you use.

The call of Ajax is well done and serves the purpose. On the server side the only thing that can fail are the consequent requests, in particular database queries. 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 could be a problem (what is usually).

In that principle, each request HTTP can utilize many server resources as your implementation seems to do. To minimise this problem, I advise you to use cache. Be it by means of software or a scheme you have designed that can save the result of your last SELECT to the database.

Something to avoid are generalist SELECT commands '*' in SQL, limiting as far as possible the number of records to return.

In programming it is not enough just to put a few commands, we have to think of the implementation as a whole, but in particular the intended purpose and the amount of data it will manage and move in each request.

Finally, it is worth saying 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 solution.

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 if you don’t have to work on old browsers (especially old ones). 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, so instead of every 1 sec the query, you consult first in this table and if there is any update then yes you query performs this your query, it will greatly decrease the server processing.

Browser other questions tagged

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