ios 倒計時在退出應用出現js暫停狀態(沒有執行),完美解決方式
ios的這個應用退出,js不執行的bug看了好多人寫的都有誤差,和偏差不是很正確,這個是優化後的
send_verify_code(){ // 驗證碼倒計時
this.count=60;//60s倒計時
this.show_timer=false;//顯示文字狀態
this.timerCodeMsg=this.count+' s';//賦值60s
var beginTime=new Date().getTime();//計算倒計時當前時間
this.timer=setInterval(() => {//定時器
var newTime=new Date().getTime();//獲取當前時間,假設退出應用
this.num++;//記入延遲時間1秒的疊加
var dTime=parseInt((newTime-beginTime)/1000-this.num);//退出應用總共時間
if (this.count > 0 && this.count <= 60) {
this.count--;
//退出應用獲取時間在走js不執行如果時間差小於0重置count為0
if(this.count-parseInt(dTime)<=0){
this.count=0
this.timerCodeMsg=(this.count)+' s';
}else{
this.timerCodeMsg=(this.count-parseInt(dTime))+' s';
}
} else {
clearInterval(this.timer);
this.show_timer=true;
this.timerCodeMsg='重發校驗碼';
this.num=0//假設時間差為負數重置為0
this.timer = null;
}
}, 1000)
},