思路
页面进入记录开始时间,开启定时器记录时长 页面离开掉接口
页面返回 和 浏览器返回 都会执行 beforeDestroy,但是无法掉接口 所以使用组件内路由守卫beforeRouteLeave,监听返回调取接口 刷新页面不会执行beforeDestroy和beforeRouteLeave,所以需要监听页面刷新 监听刷新掉接口会存在有时候接口没有掉成功就刷新完了重新加载了,导致接口没有成功 所以在页面刷新的时候将 学习时长存储,在mounted 判断本地存储有时长再调取接口
< div class = "flex pt-10" style= "height: 60px" >
< el- page- header @back= "goBack" content= "知识库" > < / el- page- header>
< div class = "flex_r" > 已学习
< span class = "size-26 green bold ml-10" { { formattedTime} } < / span>
< / div>
< / div>
mounted ( ) {
if ( localStorage. getItem ( "learnTime" ) ) {
this . setTime ( localStorage. getItem ( "learnTime" ) ) ;
localStorage. removeItem ( "learnTime" ) ;
clearInterval ( this . timer) ;
window. removeEventListener ( "beforeunload" , this . beforeunloadHandler) ;
}
this . startTime = new Date ( ) ;
this . startTimer ( ) ;
window. addEventListener ( "beforeunload" , this . beforeunloadHandler) ;
} ,
beforeDestroy ( ) {
console. log ( "beforeDestroy" ) ;
clearInterval ( this . timer) ;
window. removeEventListener ( "beforeunload" , this . beforeunloadHandler) ;
} ,
beforeRouteLeave ( to, from, next ) {
console. log ( "beforeRouteLeave" ) ;
this . $axios
. put ( "/qualityTrain/duration/record" , {
duration : this . time,
} )
. then ( ( res ) => { } )
. catch ( ( err ) => { } )
. finally ( ( ) => {
next ( ) ;
} ) ;
} ,
methods : {
startTimer ( ) {
this . timer = setInterval ( ( ) => {
const elapsedTime = new Date ( ) - this . startTime;
const hours = Math. floor ( elapsedTime / 3600000 ) ;
const minutes = Math. floor ( ( elapsedTime % 3600000 ) / 60000 ) ;
const seconds = Math. floor ( ( elapsedTime % 60000 ) / 1000 ) ;
const formattedHours = String ( hours) . padStart ( 2 , "0" ) ;
const formattedMinutes = String ( minutes) . padStart ( 2 , "0" ) ;
const formattedSeconds = String ( seconds) . padStart ( 2 , "0" ) ;
this . time = parseInt ( elapsedTime / 1000 ) ;
this . formattedTime = ` ${ formattedHours} : ${ formattedMinutes} : ${ formattedSeconds} ` ;
} , 1000 ) ;
} ,
setTime ( time ) {
if ( time) {
console. log ( this . formattedTime, this . time, time) ;
} else {
console. log ( this . formattedTime, this . time) ;
}
} ,
beforeunloadHandler ( ) {
localStorage. setItem ( "learnTime" , this . time) ;
} ,
goBack ( ) {
this . $router. go ( - 1 ) ;
} ,
}