|
// pages/bookServices/examination/questionSchedule/questionSchedule.js
|
Component({
|
/**
|
* 组件的属性列表
|
*/
|
properties: {
|
|
},
|
created() {
|
// if (props.answerType == 'option') {
|
// this.startCountdown()
|
this.setData({
|
countdownTime: 2 * 60 * 60 * 1000
|
})
|
// }
|
},
|
detached() {
|
if (this.data.countdownInterval !== null) {
|
clearInterval(this.data.countdownInterval)
|
}
|
},
|
|
/**
|
* 组件的初始数据
|
*/
|
data: {
|
countdownInterval: null, // 计时器
|
isCountdownRunning: true, // 是否倒计时
|
countdownTime: "", // 时间
|
showTime: '',
|
},
|
observers: {
|
'countdownTime': function (newValue, oldValue) {
|
const showTime = this.formatTime(this.data.countdownTime)
|
this.setData({
|
showTime: showTime
|
})
|
console.log(this.data.showTime);
|
}
|
},
|
/**
|
* 组件的方法列表
|
*/
|
methods: {
|
// 格式化时间
|
formatTime(ms) {
|
const hours = Math.floor((ms / (1000 * 60 * 60)) % 24)
|
.toString()
|
.padStart(2, '0')
|
const minutes = Math.floor((ms / (1000 * 60)) % 60)
|
.toString()
|
.padStart(2, '0')
|
const seconds = Math.floor((ms / 1000) % 60)
|
.toString()
|
.padStart(2, '0')
|
return `${hours}:${minutes}:${seconds}`
|
},
|
// 获取保存的倒计时时间
|
getSavedTime() {
|
const savedTime = wx.getStorageSync('countdownTime')
|
return savedTime ? parseInt(savedTime) : null
|
},
|
// 保存倒计时时间到本地存储
|
saveTime() {
|
wx.setStorageSync('countdownTime', this.data.countdownTime.toString())
|
},
|
clearTime() {
|
this.setData({
|
countdownTime: 2 * 60 * 60 * 1000
|
})
|
},
|
// 暂停或继续倒计时
|
toggleCountdown() {
|
if (countdownInterval) {
|
clearInterval(this.data.countdownInterval)
|
this.setData({
|
countdownInterval: null,
|
isCountdownRunning: false
|
})
|
} else {
|
this.startCountdown()
|
this.setData({
|
isCountdownRunning: true
|
})
|
}
|
},
|
// 开始倒计时
|
startCountdown() {
|
// 如果计时器已经存在,先清除之前的计时器
|
if (this.data.countdownInterval) {
|
clearInterval(this.data.countdownInterval)
|
this.setData({
|
countdownInterval: null
|
})
|
}
|
this.setData({
|
countdownInterval: setInterval(() => {
|
this.setData({
|
countdownTime: this.data.countdownTime - 1000
|
})
|
if (this.data.countdownTime <= 0) {
|
clearInterval(this.data.countdownInterval)
|
this.setData({
|
countdownTime: 0,
|
isCountdownRunning: false
|
})
|
}
|
this.saveTime()
|
}, 1000)
|
})
|
|
}
|
}
|
})
|