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 long polling.
Update: 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.
Something like this:
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.