Cómo obtener datos en tiempo real de la base de datos usando jQuery cada 1 segundo

Como llegar en tiempo real data from mySQL database using jQuery every Una segunda

i need change document.write so i see result in new page

<SCRIPT>
            setTimeout("document.write('<p><?php
        $qry = mysql_query('SELECT SUM(clicks) AS total FROM short_urls');
        $row = mysql_fetch_assoc($qry);
        echo number_format($row['total']); ?></p>')",1000);
</SCRIPT>

preguntado el 08 de enero de 11 a las 16:01

Please, make sure you really understand why the code above is a total nonsense. You except the browser to run code that is designed to run on your server. -

2 Respuestas

A simple strategy is to use setTimeout:

setTimeout( function(){
  your_function_that_gets_data();
  your_function_that_displays_data();
}, 1000 );

For more sophisticated approaches, you might look into sondeo largo.


Actualizar: Anas, it looks like you went off the rails a bit after I suggested the above option. Whether you use setTimeout() or setInterval(), you certainly want to avoid the confusing mix with which you updated your question.

Make a php page that simply returns the total. Write your JavaScript to call that page asynchronously (i.e., via ajax), at whatever interval you like. When the result is returned, use a callback function to redraw the page region so the total is updated for the user.

Algo como esto:

var updateTotal = function () {
  $.get('your_script_that_returns_a_total.php', function( data ){ // get new total
    $('#div_displaying_total').val( data ); // paint a div with the new result
  });
};

Now you have a simple function that, when run, will update a div in your view with the current total. Run that function as frequently as you like. For example, to run it every five seconds:

var running = setInterval( updateTotal, 5000 );

You can of course get much fancier with timing, writing your updateTotal function to call itself after a delay, etc. And you can stop the updating like this:

clearInterval( running );

...which allows you to easily set up an on/off switch.

If any of this leads to greater clarity, I'd suggest updating your question, which is attracting downvotes for the muddled code therein.

contestado el 23 de mayo de 17 a las 15:05

You might also want to consider setInterval(). It repeats it automatically. And to prevent to many requests at a time you should put a setTimeout at the end of the return of each request. - Eric

How is this code for streamlining, I mean will this clog up my server requests ? - MarkP

You will have to use PHP to make a call to the database to read all the rows out of it. The PHP would be called by an AJAX event set using a JavaScript setTimeout() function for every 1000ms (1 second). The huuuuge issue I can see with this is if you have many users, a large database and lots of simultaneous logins, you will very quickly overload your server. I think submitting a request every 30 seconds or something would be better, maybe 10 at the least.

Respondido el 08 de enero de 11 a las 19:01

thank you.. i will add this code in "About" page i think This will not hurt much because the number of visited this page a little - Abudayah

A nice idea, although just to be safe I'd only update at minimum every 10 seconds. - Bojangles

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.