延时器

This commit is contained in:
蔡玲
2024-09-20 13:28:39 +08:00
parent 5bec011106
commit 25d79011dc

73
main.js
View File

@@ -26,37 +26,64 @@ Vue.directive('enterNumber', {
}); });
// 全局计时 // 全局计时
// Vue.prototype.$globalData = {
// timer: null,
// timerDuration: 20, // 假设是60秒
// startTimer: function() {
// const self = this;
// self.timer = setInterval(function() {
// self.timerDuration--;
// console.log('剩余时间:' + self.timerDuration + '秒');
// if (self.timerDuration <= 0) {
// clearInterval(self.timer);
// self.timerDuration = 20;
// console.log('计时器结束1');
// uni.showModal({
// title: '提示',
// showCancel: false,
// content: '路线锁定未释放,请操作完成再退出!',
// success: (res) => {
// if (res.confirm) {
// Vue.prototype.$globalData.startTimer()
// }
// }
// })
// }
// }, 1000);
// },
// stopTimer: function() {
// if (this.timer) {
// clearInterval(this.timer);
// console.log('计时器结束2');
// this.timer = null;
// this.timerDuration = 20;
// }
// }
// };
Vue.prototype.$globalData = { Vue.prototype.$globalData = {
timer: null, timer: null,
timerDuration: 20, // 假设是60秒
startTimer: function() { startTimer: function() {
const self = this; const self = this;
self.timer = setInterval(function() { self.timer = setTimeout(function() {
self.timerDuration--; clearTimeout(self.timer);
console.log('剩余时间:' + self.timerDuration + '秒'); // console.log('延时器结束1');
if (self.timerDuration <= 0) { uni.showModal({
clearInterval(self.timer); title: '提示',
self.timerDuration = 20; showCancel: false,
console.log('计时器结束1'); content: '路线锁定未释放,请操作完成再退出!',
uni.showModal({ success: (res) => {
title: '提示', if (res.confirm) {
showCancel: false, Vue.prototype.$globalData.startTimer()
content: '路线锁定未释放,请操作完成再退出!', }
success: (res) => { }
if (res.confirm) { })
Vue.prototype.$globalData.startTimer() }, 10000);
}
}
})
}
}, 1000);
}, },
stopTimer: function() { stopTimer: function() {
if (this.timer) { if (this.timer) {
clearInterval(this.timer); clearTimeout(this.timer);
console.log('计时器结束2'); // console.log('延时器结束2');
this.timer = null; this.timer = null;
this.timerDuration = 20;
} }
} }
}; };