// Document.visibilityState 可以监听用户是否离开页面,在返回页面
var t1, {log} = console;
document.addEventListener("visibilitychange", function() {
if(this.visibilityState === 'hidden' ){
t1 = new Date().getTime();
log('页面对用户不可见.')
}
if( this.visibilityState === 'prerender'){
log('页面此时正在渲染中')
}
if( this.visibilityState === 'visible' ){
log( ~~((Date.now() - t1) / 1000) )
log('页面内容至少是部分可见')
}
if(this.visibilityState === 'unloaded'){
log('页面从内存中卸载清除')
}
});
setInterval(()=>{
console.log( document.hasFocus() );
}, 200)