短信验证码跨页面倒计时

// 在销毁页面时记录当前时间戳
onUnload() {
    
    
	// 销毁当前页面定时器
		if (this.c) {
    
    
			clearInterval(this.c)
			//记录当前返回到登录页时重新获取短信验证码倒计时时间戳
			this.$store.state.codeTime = new Date().getTime();
			this.c = null;
		}
},
// 在加载页面时间判断当前时间和上一次销毁时间的差值
onLoad(option) {
    
    
	let {
    
    
		phone,
		time,
	} = option;
	this.phone = phone;
	if(this.$store.state.codeTime){
    
    
		// 再次进入的时间比上一次获取短信验证码倒计时低于60秒则继续倒计时
		const newTime = new Date().getTime(); // 当前时间
		const codeTime = this.$store.state.codeTime; // 上一次时间
		const diffTime = parseInt((newTime - codeTime)/1000)
		if(diffTime< 60) {
    
    
			this.time = parseInt(60-diffTime);
			this.openCodeTimer(this.time)
		}else{
    
    
			this.getCode();
		}
	}else {
    
    
		this.getCode();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_38566069/article/details/130259617