Refresh Page Automatically

I will be creating a JQuery slide show, but need the page to refresh every 5 minutes to change the content.

I know I can do this via some Javascript, but this can be altered on the client side to avoid page refreshes. Is there a way for the server to timeout a page and force a refresh?

Answers


In addition to a javascript reload, you can send a refresh header:

header("Refresh: 300;url='http://thepage.com/example'");

The browser will redirect after 300 seconds regardless of javascript. It can be disabled in configuration of the browser though, but it's not commonly disabled.


Meta refresh will work very simply:

<meta http-equiv="refresh" content="300">

The 300 means it will refresh every 300 seconds or 5 minutes. In this way you don't use JavaScript so there is no way the user can turn it off.


There's no way you can force the page to reload from the server.

The way to do it in JavaScript is simple:

setTimeout(function() {
    window.location.reload();
}, 5000);

Although this could be overridden on the client side, reguler users won't be messing around with your script.


As a side note: Is there any reason you're so adamant on reloading the page?


The JavaScript Method

var timer = null;

function auto_reload()
{
  window.location = 'http://domain.com/page.php';
}

<body onload="timer = setTimeout('auto_reload()',10000);">

The META Tag Method

The following refreshes the page every 30 seconds.

<head>
<meta http-equiv="refresh" content="30" />
</head>

Using PHP

I would use a php function that can be called on any page without having to set the url

    // write the function
    function refresh( $time ){
        $current_url = $_SERVER[ 'REQUEST_URI' ];
        return header( "Refresh: " . $time . "; URL=$current_url" );
    }

    // call the function in the appropriate place
    refresh( 4 );   
    // this refreshes page after 4 seconds

Another way to do that reload is using window.location:

    setTimeout(function(){
        window.location.href = "YOUR_PAGE_URL";
    }, 5000);

Need Your Help

Determining if an Activity exists on the current device?

android android-activity

Is there a way to check and see if an Activity exists on your device? If I have a youtube video link I want to specify it open in the YouTube PlayerActivity. However, I don't want to crash if for s...

How to Create Singleton Object in swift

ios swift singleton

I am learning about Singleton pattern in swift and efficient way to create a Singleton class and found out the best way to create as below.