How to update Mysql results automatically with Javascript?

Asked

Viewed 240 times

0

How do I update Mysql queries automatically with Javascript?

I was looking at the site and I found something that helped me just that I put on my site and when I went to see the server processes the second query.php file was opening more than 150 processes (leaving the server super slow) there is some more practical way to solve my problem?

<script type="text/javascript">
function doRefresh(){
    $("#contagem").load("consulta.php");
}
$(function() {
    setInterval(doRefresh, 100);
});
</script>

Query file.php

<?php
require_once "config.php";

$data = mysql_fetch_array(mysql_query("SELECT * FROM teste WHERE ativo='1' ORDER BY RAND() LIMIT 1;")); 
$t = time() - $data['tempo'];   
echo "<font color='green'><h1>Tempo: $t</h1></font>";
mysql_close($mysql); ?>
  • How about increasing the time to 500 milliseconds or more? It is really necessary that this query is made 10 times in 1 second?

  • Next, the "setInteval" causes all the code within it to be repeated and that '100' is the delay time in which this code repeats. Since it is asynchronous, it will generate call above call making the server slow. My suggestion is that you increase this setInterval time. look for how long it takes the select to be done and put that time average a little higher if possible

  • The most practical way to work with real-time data is to create a websocket service. That’s why there are millions of implementations like nodejs. Search on the subject right here in the OS, there are several questions about it. I just don’t recommend working with PHP socket, because they may bring some memory problems.

1 answer

1

Legal if possible was to change to setInterval(doRefresh, 1000); And make sure your server has automatic connection shutdown if you don’t have to do it via code.

Browser other questions tagged

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