前端页面上,我们的需求是比如6点10分更新一次第一个图表,6点20更新一次,6点30更新一次,6点40,6点50。就是每到整分钟就更新一次。然后每天的8点半,更新一第二个图表的数据(也就是重新调用一下接口方法),但是我现在想知道怎么判断整点和8点半这两个时刻啊?
4条回答 默认 最新
- ZionHH 2022-01-17 08:20关注
var minTimer = null var hourTimer = null // 整分 function timerFunc() { var date = new Date() // 取当前分钟个位数,方便计算 var mins = date.getMinutes() % 10 // 触发时间:10 - mins 求出剩余时间 var ticktack = (10 - mins) * 60 * 1000 minTimer = setTimeout(timerFunc, ticktack) if (mins == 0) { console.log('整分了') } } timerFunc() // 8:30 function hourTimerFunc() { const date = new Date() const hours = date.getHours() const mins = date.getMinutes() const now = date.getTime() const targetTime = new Date().setHours(8, 30, 0) // 剩余时间 let remTime = targetTime > now ? (targetTime - now) : (targetTime + 86400000 - now) hourTimer = setTimeout(hourTimerFunc, remTime) if (hours === 8 && mins === 30) { console.log('8:30了') } } hourTimerFunc()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录