让页面不缓存,自动刷新

目的:让页面不缓存,自动刷新。

问题

前端做了一个计时器需要实时从后端获取数据,数据是后端塞到jsp里面的。
我从另一个页面返回该页面后,页面从缓存中取的数据,导致计时出错。
在这里插入图片描述

解决

1.添加meta标签设置不使用缓存

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

2.添加刷新操作

window.onpageshow = function(event) {
	if (event.persisted || window.performance && window.performance.navigation.type == 2) {//页面是从缓存中获取的数据||是通过浏览器后退来到该页面
		window.location.reload();
	}
}
  • window.performance.navigation.type == 2 通过后退返回该页面
  • event.persisted 从缓存获取数据时为true,否则为false

拓展

window.performance

  • navigation字段统计的是一些网页导航相关的数据:

  • redirectCount:重定向的数量(只读),但是这个接口有同源策略限制,即仅能检测同源的重定向;

  • type 返回值应该是0,1,2 中的一个。分别对应三个枚举值:

    • 0 : TYPE_NAVIGATE (用户通过常规导航方式访问页面,比如点一个链接,或者一般的get方式)
    • 1 : TYPE_RELOAD (用户通过刷新,包括JS调用刷新接口等方式访问页面)
    • 2 : TYPE_BACK_FORWARD (用户通过后退按钮访问本页面)

本文链接:https://blog.csdn.net/qq_39903567/article/details/114981256

猜你喜欢

转载自blog.csdn.net/qq_39903567/article/details/114981256
今日推荐