页面的生命周期
浏览器页面的生命周期
应用程序生命周期是现代操作系统管理资源的一种重要方式。在
事实上,今天的浏览器已经在主动采取措施为后台标签页中的页面节约资源,许多浏览器(尤其是
- 引入网页生命周期状态的概念并使之标准化。
- 定义新的、由系统发起的状态,允许浏览器限制隐藏的或不活动的标签消耗的资源。
- 创建新的
API 和事件,使Web 开发人员能够响应这些新的系统启动状态的转换。
该解决方案提供了网络开发者构建应用程序对系统干预的弹性所需的可预测性,它允许浏览器更积极地优化系统资源,最终使所有网络用户受益。
生命周期概述
所有的页面生命周期状态都是离散的、互斥的,也就是说一个页面一次只能处于一种状态。而页面生命周期状态的大部分变化一般都可以通过

APIs
const getState = () => {
if (document.visibilityState === "hidden") {
return "hidden";
}
if (document.hasFocus()) {
return "active";
}
return "passive";
};
// Stores the initial state using the `getState()` function (defined above).
let state = getState();
// Accepts a next state and, if there's been a state change, logs the
// change to the console. It also updates the `state` value defined above.
const logStateChange = (nextState) => {
const prevState = state;
if (nextState !== prevState) {
console.log(`State change: ${prevState} >>> ${nextState}`);
state = nextState;
}
};
// These lifecycle events can all use the same listener to observe state
// changes (they call the `getState()` function to determine the next state).
["pageshow", "focus", "blur", "visibilitychange", "resume"].forEach((type) => {
window.addEventListener(type, () => logStateChange(getState()), {
capture: true,
});
});
// The next two listeners, on the other hand, can determine the next
// state from the event itself.
window.addEventListener(
"freeze",
() => {
// In the freeze event, the next state is always frozen.
logStateChange("frozen");
},
{ capture: true }
);
window.addEventListener(
"pagehide",
(event) => {
if (event.persisted) {
// If the event's persisted property is `true` the page is about
// to enter the Back-Forward Cache, which is also in the frozen state.
logStateChange("frozen");
} else {
// If the event's persisted property is not `true` the page is
// about to be unloaded.
logStateChange("terminated");
}
},
{ capture: true }
);