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)
      }
    })
}
相关推荐
Anastasiozzzz14 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
Highcharts.js15 小时前
数据之美:用Highcharts打造专业级弧线图
javascript·数据库·highcharts·图表开发·弧线图
mclwh15 小时前
关于React-Konva 报:Text components are not supported....错误的问题
前端·react.js
SuperEugene15 小时前
错误处理与 try/catch:真实项目里应该捕什么错?
前端·javascript·面试
Amumu1213816 小时前
CSS引入方式
前端·css
我是Superman丶16 小时前
【Demo】✋ 数字手势识别 Html
前端·html
HelloReader16 小时前
Leptos + Tauri 2 前端配置Trunk + SSG + 移动端热重载一次打通(Leptos 0.6 口径)
前端
HelloReader16 小时前
Next.js + Tauri 2 用 Static Export 把 React 元框架装进桌面/移动端
前端
Wect17 小时前
从输入URL到页面显示的完整技术流程
前端·面试·浏览器
没有bug.的程序员17 小时前
自动化测试之魂:Selenium 与 TestNG 深度集成内核、Page Object 模型实战与 Web UI 交付质量指南
前端·自动化测试·selenium·ui·testng·page·object