asp-smart-ui-plus-test
Version:
A Component Library for Vue 3
70 lines (69 loc) • 2.06 kB
JavaScript
import local from './localStorage'
export default {
data () {
return {
count: '', // 倒计时
timer: null // 保存倒计时
}
},
created () {},
methods: {
// 获取倒计时
getCountDown (locksecends) {
// 浏览器 cookie 获取时间
const now = new Date().getTime()
const djsendtime = local.get('YzmCodeTime')
if (!djsendtime || djsendtime === 'undefined' || djsendtime === 'null') {
this.setCountDown(locksecends)
return locksecends
} else {
const countDown = parseInt((djsendtime - now) / 1000)
return countDown > 0 ? countDown : locksecends
}
},
// 设置倒计时
setCountDown (locksecends) {
const now = new Date().getTime() // 获取当前时间搓
const endtime = locksecends * 1000 + now // 结束时间搓
local.set('YzmCodeTime', endtime)
},
// 清空倒计时
clearCountDown () {
local.set('YzmCodeTime', null)
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
},
// 正在倒计时
async doCoutDown () {
this.timer = setInterval(() => {
this.count--
// 设置时间
this.setCountDown(this.count)
// 业务逻辑
this.handleCoutDown()
if (this.count === 0) {
this.clearCountDown()
}
}, 1000)
},
// 是否启动倒计时
isStartCountDown () {
this.count = this.getCountDown(this.countLength ? this.countLength : 60)
this.doCoutDown()
},
// 判断是否有倒计时
isCountDown () {
// 浏览器 cookie 获取时间
const now = new Date().getTime()
const djsendtime = local.get('YzmCodeTime')
if (!djsendtime || djsendtime === 'undefined' || djsendtime === 'null') {
return null
} else {
const countDown = parseInt((djsendtime - now) / 1000)
return countDown < 0 ? null : countDown
}
}
}
}