vue实现倒计时
在assets下新建tool.js文件
function countDownFun(time) { time--; let nowTime = new Date().getTime(); // 获取当前时间 if (time >= nowTime) { var secondTime = 0 //秒 var minuteTime = 0; // 分 var hourTime = 0; // 小时 var today = 0 //天 // 全部剩余多少秒 var seconds = Math.ceil((time - nowTime) / 1000) hourTime = Math.floor(seconds / 3600) //天数 today = Math.floor(hourTime / 24) //小时 hourTime = Math.floor(hourTime % 24) < 10 ? '0' + Math.floor(hourTime % 24) : Math.floor(hourTime % 24) // 分 minuteTime = Math.floor(seconds / 60 % 60) < 10 ? '0' + Math.floor(seconds / 60 % 60) : Math.floor(seconds / 60 % 60) //秒 secondTime = Math.floor(seconds % 60) < 10 ? '0' + Math.floor(seconds % 60) : Math.floor(seconds % 60) return today + 'd' + hourTime + 'h' + minuteTime + 'm' } else { return "00d 00h 00m"; } } export function initCountdown(endTime, callback) { let time = '00d 00h 00m' let Interval = '' if (endTime - Date.now() > 0) { time = countDownFun(endTime); //首次 //定时器 Interval = setInterval(function () { if (endTime - Date.now() <= 0) { clearInterval(Interval); time = "00d 00h 00m"; } else { time = countDownFun(endTime); } callback(time) }, 1000); } else { callback(time) } }
在需要使用的地方引入 initCountdown
initCountdown('目标时间戳', (reslove)=>{ this.time = reslove //返回的结果 })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-04-27 element 设置table表头样式
2020-04-27 js使用正则表达式匹配富文本框中的img标签