自动刷新页面

时间:2012-07-22 21:53:13

标签: php javascript jquery html

我将创建一个JQuery幻灯片放映,但需要每5分钟更新一次页面以更改内容。

我知道我可以通过一些Javascript来做到这一点,但这可以在客户端进行更改,以避免页面刷新。有没有办法使服务器超时页面并强制刷新?

7 个答案:

答案 0 :(得分:21)

除了重新加载javascript之外,您还可以发送刷新标头:

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

无论javascript如何,浏览器都会在300秒后重定向。虽然可以在浏览器的配置中禁用它,但它通常不会被禁用。

答案 1 :(得分:15)

元刷新将非常简单:

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

300表示每300秒或5分钟刷新一次。这样您就不会使用JavaScript,因此用户无法将其关闭。

答案 2 :(得分:5)

您无法强制从服务器重新加载页面。

在JavaScript中实现它的方法很简单:

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

虽然这可以在客户端覆盖,但是调整用户不会搞乱你的脚本。


作为旁注:在重新加载页面时,您是否有任何理由坚持

答案 3 :(得分:1)

JavaScript方法

var timer = null;

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

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

答案 4 :(得分:1)

META代码方法

The following refreshes the page every 30 seconds.

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

答案 5 :(得分:1)

使用PHP

我会使用一个可以在任何页面上调用的php函数,而无需设置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

答案 6 :(得分:-1)

另一种重新加载的方法是使用window.location

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