www.pokeroconnor.com

Javascript Auto Refresh Page

April3

Thanks to this excellent blog, this is how to get a page to auto refresh on a timer using javascript (in this example its on a 10 second timer):

<script type=”text/javascript” language=”javascript”> 

var reloadTimer = null;

window.onload = function()

{

    setReloadTime(10); // In this example we’ll use 10 seconds.

}

function setReloadTime(secs) 

{

    if (arguments.length == 1) {

        if (reloadTimer) clearTimeout(reloadTimer);

        reloadTimer = setTimeout(”setReloadTime()”, Math.ceil(parseFloat(secs) * 1000));

    }

    else {

        location.reload();

    }

}

</script>

And you can do the following to add a link to refresh the page:

<a href=”javascript:location.reload();”>Refresh the page</a>

reload() refreshes from the cache, so if you want to force it to get a fresh copy via GET from the server, pass ‘true’ as a param. E.g. <a onclick=”location.reload(true)” href=”#”>Refresh the page</a>

You can also do :<meta http-equiv=Refresh” content=n;url”/>, just leave off the url if you want to refresh to the current page.

posted under jQuery & JS

Email will not be published

Website example

Your Comment: