思路:
// verifyCodeDisable布尔值:控制按钮是否禁用的状态 // time倒计时,默认0 // overtime结束的时间,默认0 // Date.parse(new Date()) / 1000获取现在的时间戳,去除后面三位0 // 生命周期 mounted() { let _this = this uni.getStorage({ key: ‘phone‘, success: function (res) { _this.overtime = res.data } }); console.log("shijian:"+this.overtime) // 判断overtime是否存在,这里是一个时间戳,其实无所谓,不等于0就行 if(this.overtime > 1122772662) { // 得到time为负数 this.time = this.overtime - Date.parse(new Date()) / 1000 if (this.time < 0) { this.time = this.time + 60 // 按钮是否禁用 this.verifyCodeDisable = true this.codedownTime() } } methods: { // 定时器 codedownTime(){ let _this = this const count = setInterval(() => { this.time-- if (this.time <= 0) { clearInterval(count) _this.verifyCodeDisable = false uni.removeStorageSync(‘phone‘) } }, 1000) } // 获取验证码事件 sendVerifyCode() { let _this = this this.verifyCodeDisable = true // 初始时间,60秒 this.time = 60 uni.setStorage({ key: ‘phone‘, data: Date.parse(new Date()) / 1000 }) _this.codedownTime() }
原文:https://www.cnblogs.com/black-eyes/p/14852652.html