带有警报的JavaScript页面重新加载计时器

时间:2016-06-14 15:21:54

标签: javascript jquery twitter-bootstrap

我要做的是在重新加载页面59分钟后,在页面重新加载后显示一条消息,指出页面已重新加载。然后再次设置计时器并重复相同的操作。我在隐藏和显示警报方面遇到了麻烦,并且在第一次刷新后的逻辑上也有一点丢失。 这就是我到目前为止所做的:

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.Bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jQuery/1.12.2/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<script language="JavaScript"> <!--
function checkRefresh()
{
    if( document.refreshForm.visited.value == "" )
    {
        // This is a fresh page load
        document.refreshForm.visited.value = "1";
  document.getElementById(a).style.visibility="hidden";
    window.setTimeout(myalertfunction, 59 * 60 * 1000)
    }
    else
    {
document.getElementById(a).style.visibility="visible";
        // This is a page refresh
window.setTimeout(myalertfunction, 59 * 60 * 1000)
    }
} -->
</script>

</head>

<body onLoad="JavaScript:checkRefresh();">
<div class="alert alert-info fade in" id="a">
    <a href="#" "class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>Info!</strong> This Page has been reloaded!
  </div>
<form name="refreshForm">
<input type="hidden" name="visited" value="" />
</form>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试使用<meta>代码

<meta http-equiv="refresh" content="5; URL=http://www.example.com/test.html">

或JavaScript setTimeout()

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

JSFiddle Demo

答案 1 :(得分:0)

也许你可以尝试这样的事情:

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.Bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<script language="JavaScript"> 
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

$(document).ready(function() {
    var visitedValue = getParameterByName('visited',window.location.href);
    if (visitedValue === '1') {
      $('#a').show();
    }
    setTimeout(function(){

      $('#visited').val('1');
      $('#refreshForm').action = window.location.href;
      $('#refreshForm').submit();
    },3540000);
});
</script>

</head>

<body>
<div class="alert alert-info fade in" id="a" style="display:none;">
    <a href="#" "class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>Info!</strong> This Page has been reloaded!
  </div>
<form name="refreshForm" id="refreshForm">
  <input type="hidden" id="visited" name="visited" value="" />
</form>
</body>
</html>