5秒后刷新一次网页

时间:2011-11-21 18:24:57

标签: refresh webpage

我正在寻找一个JavaScript解决方案(或其他任何东西),它只会在打开5秒后刷新一次网页。这可能不会卡在刷新循环中吗?

5 个答案:

答案 0 :(得分:3)

试试这个:

setTimeout(function ()
    {
        if (self.name != '_refreshed_'){
        self.name = '_refreshed_';
        self.location.reload(true);
    } else {
        self.name = ''; 
    }
    }, 5000);

答案 1 :(得分:2)

你可以用很多不同的方式做到这一点,但我认为最简单的方法是在刷新后向url添加一个查询字符串,让我们知道是否已经发生了刷新: / p>

//Function to get query string value. Source: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
function getQuerystring(key, default_){
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

//check if our query string is already set:
if(getQuerystring(r) !== 1){
  setTimeout(function(){window.location.href = window.location.href + '?r=1'},5000)
}

如果可能存在查询字符串,则必须考虑该字符串并更改“?”到'&'。

答案 2 :(得分:0)

您只需要在页面加载之间传递某种数据。这可以通过多种方式完成 - 使用cookie,URL查询参数或服务器端的某些内容。查询参数示例:

if (!location.search.match(/(\?|&|^)stopRefreshing(=|&|$)/))
{
    setTimeout(function ()
    {
        var search = location.search;
        location.search = search ? search + '&stopRefreshing' : 'stopRefreshing';
    }, 5000);
}

演示:http://jsbin.com/ofawuz/edit

答案 3 :(得分:0)

当然,如果您不介意使用jquery,可以在等待5秒后通过ajax调用完成。只是给你一些示例代码:

How to wait 5 seconds with jQuery?

$(document).ready(function() {

        // Get data
        $.ajax({
            url : '/tommyStockExchange/Data',
            dataType : 'html',
            data : {
                'format' : 'H',
                'type' : 'E'
            },
            success : function(data) {
                $("#executions").html(data);
            },
            statusCode : {
                404 : function() {
                    alert('executions url 404 :(');
                }
            }

        });

    });

答案 4 :(得分:0)

使用不同的#hash将其重定向到同一页面,如果未设置哈希,则只在JS中注册重定向。