vue 设置定时器在某个时间段执行

业务需求:数据大屏页面中的某个方法需要在今天12:00执行一次,或是在今天的17:00执行一次,

这里用到 setTimeout定时器来实现。

java 复制代码
//获取某个时间的时间戳   
 getCurrentDate(time) {
      let now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      let day = now.getDate();
      return year + "-" + month + "-" + day + ' ' + time
  },

mounted(){
    let timeList = ['7:35:00', '17:05:00']
    timeList.forEach(item => {
      let nowtime = new Date().getTime()
      let onetime = new Date(this.getCurrentDate(item)).getTime()
      if (nowtime > onetime) {
        return
      } else {
        let t = (onetime - nowtime)
        setTimeout(() => {
          //要执行的方法
        }, t)
      }
    })
}
相关推荐
yinuo12 分钟前
CSS基础动画keyframes
前端
一条上岸小咸鱼1 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
前端小巷子1 小时前
Vue 事件绑定机制
前端·vue.js·面试
uhakadotcom1 小时前
开源:subdomainpy快速高效的 Python 子域名检测工具
前端·后端·面试
爱加班的猫2 小时前
Node.js 中 require 函数的原理深度解析
前端·node.js
用户8165111263972 小时前
WWDC 2025 Build a SwiftUI app with the new design
前端
伍哥的传说2 小时前
Vue 3.5重磅更新:响应式Props解构,让组件开发更简洁高效
前端·javascript·vue.js·defineprops·vue 3.5·响应式props解构·vue.js新特性
阅文作家助手开发团队_山神2 小时前
第一章: Mac Flutter Engine开发准备工作
前端·flutter
菜牙买菜2 小时前
Hicharts入门
前端·vue.js·数据可视化