如何在30秒后刷新页面5次?

时间:2019-07-19 11:53:10

标签: javascript php html

我想在5秒后每次刷新页面30次。 我无法使用document.location.reload() js函数,因为当再次执行js时会重新加载整个页面。 有什么想法吗?

3 个答案:

答案 0 :(得分:4)

使用sessionStorage

let refreshNum = sessionStorage.getItem('refreshNum') || 0;

if(refreshNum<5) {
  console.log('before 5 refresh');
  refreshNum++;
  sessionStorage.setItem('refreshNum', refreshNum);
  // refresh your page here
  setTimeout(()=> document.location.reload(), 30000)
} else {
  console.log('after 5 refresh');
}

答案 1 :(得分:1)

使用php,您可以使用会话存储时间+提前5 * 30秒。
然后使用html meta refresh重新加载页面

session_start(); // not in head. Must be at the top of the document



if(!isset($_session['time'])){
    $_session['time'] = time() +150;
}else{
    if(time() < $_session['time']) echo "<meta http-equiv='refresh' content='30'>";
}

并将其放在html的开头。

因为这是服务器端的功能,当客户端关闭JavaScript时也可以使用。

答案 2 :(得分:0)

您可以尝试使用url中的get参数重新加载页面:

window.location.href = window.location.href + "?reloadCount=0";

然后在页面加载30秒后:

var urlParams = new URLSearchParams(window.location.search);
var reloadCount = urlParams.get('reloadCount');

如果reloadCount> = 5,则下一次停止重新加载页面,否则添加1以重新加载计数并使用更新的参数表重新加载页面

window.location.href = window.location.href + "?reloadCount=1";