首页 > 其他分享 >VUE定时器任务(每天12点执行)

VUE定时器任务(每天12点执行)

时间:2023-03-08 12:58:36浏览次数:35  
标签:VUE 60 定时器 var 12 执行 config intervalTimer

原文链接:https://blog.csdn.net/ITERCHARLIE/article/details/124447463

设定配置data

data() {
config: {
time: '00:00:00', // 每天几点执行
interval: 1, // 隔几天执行一次
runNow: true, // 是否立即执行
intervalTimer: '',
timeOutTimer: ''
}
}
getBussTop10DataByTimer() {
if (this.config.runNow) {
// 如果配置了立刻运行则立刻运行任务函数
this.initTopTenBusiness()
}
// 获取下次要执行的时间,如果执行时间已经过了今天,就让把执行时间设到明天的按时执行的时间
var nowTime = new Date().getTime()
var timePoint = this.config.time.split(':').map((i) => parseInt(i))

var recent = new Date().setHours(...timePoint) // 获取执行时间的时间戳

if (recent <= nowTime) {
recent += 24 * 60 * 60 * 1000
}
// 未来程序执行的时间减去现在的时间,就是程序要多少秒之后执行
var doRunTime = recent - nowTime
this.config.timeOutTimer = setTimeout(this.setTimer, doRunTime)
},
setTimer() {
console.log('进入定时器')
//配置后的第一天12点执行
this.initTopTenBusiness()
// 每隔多少天再执行一次
var intTime = this.config.interval * 24 * 60 * 60 * 1000
this.config.intervalTimer = setInterval(this.initTopTenBusiness, intTime)
}
清除定时器

beforeDestroy() {
console.log(
'关闭任务定时器',
this.config.intervalTimer
)
clearInterval(this.config.intervalTimer)
console.log('清除定时器timeout', this.config.timeOutTimer)
clearTimeout(this.config.timeOutTimer)
}

标签:VUE,60,定时器,var,12,执行,config,intervalTimer
From: https://www.cnblogs.com/fswhq/p/17191651.html

相关文章