Refresh a page every minute with Javascript

If you are building a Dashboard or anyking of website that work with live data, in many case you will need to refresh the page every X seconde.

In many situation I was building a dashboard with data about an ecommerce site.
I need to update the # of sale, amount of sale, last time the server sync with the POS …

Everytime I refresh the page all the script run an the page load with the latest data.
I know this is not the best method to do this, but in my situation it’s a quick fix that help my client to stop hit the refresh button of the browser.

Here is the simple code that refresh the page everymine.

If you need the page to refresh other then every minute you can change the 60000 milisecond value to any value you need.
Don’t forget to replace the value at the 2 places.

<script type="text/javascript">
var timeout = setTimeout("location.reload(true);",60000);
function resetTimeout() {
clearTimeout(timeout);
timeout = setTimeout("location.reload(true);",60000);
}
</script>

If you need to incorporate a refresh button on your page you only need to call the function like so:

<a href="javascript:;" onclick="resetTimeout();">REFRESH</a>

Advertise with us!