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)
      }
    })
}
相关推荐
pandarking2 分钟前
[CTF]攻防世界:web-unfinish(sql二次注入)
前端·数据库·sql·web安全·ctf
IT_陈寒2 分钟前
Java并发编程避坑指南:从volatile到ThreadLocal,8个实战案例解析线程安全核心原理
前端·人工智能·后端
ByteCraze7 分钟前
前端性能与监控指标采集系统设计方案
前端
山楂树の13 分钟前
前端实时渲染性能优化 使用cocoRLE编码进行图像传输并着色绘制
前端·图像处理·实时互动
程序员祥云14 分钟前
云希谷技术面
前端
疯笔码良18 分钟前
Vue + Echarts 实现科技感数据大屏
vue.js·echarts
一 乐21 分钟前
心理健康管理|基于springboot + vue心理健康管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
G***E31621 分钟前
重塑前端的力量:从界面承载到体验驱动的全链路能力进化
前端
HIT_Weston21 分钟前
60、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(四)
前端·ubuntu·gitlab
G***E31623 分钟前
前端技术的下一场深变:从工程化到智能化的全面升级
前端